This example binds focused rows in master and detail grids to View Model properties. The focused row's name for each nested level is displayed below the GridControl:
You can bind CurrentItem, SelectedItem, and SelectedItems properties for master and detail grids to View Model properties.
Files to Review
- Window1.xaml (VB: Window1.xaml)
- ViewModel.cs (VB: ViewModel.vb)
Documentation
More Examples
- WPF Data Grid - Create Master-Detail Grid
- WPF Data Grid - Display Detail Content in Tabs
- WPF Data Grid - Expand and Collapse Master Rows
- WPF Data Grid - Display Different Details Based on Data in the Master Row
- WPF Data Grid - Use the ParentPath Property to Define the Selected Detail Row in the View Model
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
XAML<Window
x:Class="MasterDetailInside.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:local="clr-namespace:MasterDetailInside"
Title="Window1"
Width="700"
Height="500">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Window.Resources>
<Style TargetType="dxg:TableView">
<Setter Property="ShowGroupPanel" Value="False" />
</Style>
</Window.Resources>
<DockPanel>
<StackPanel DockPanel.Dock="Bottom">
<TextBlock Text="{Binding Level1CurrentItem.Name, FallbackValue=NONE}" />
<TextBlock Text="{Binding Level2CurrentItem.Name, FallbackValue=NONE}" />
<TextBlock Text="{Binding Level3CurrentItem.Name, FallbackValue=NONE}" />
</StackPanel>
<dxg:GridControl
AutoGenerateColumns="AddNew"
CurrentItem="{Binding Level1CurrentItem}"
ItemsSource="{Binding Data}">
<dxg:GridControl.DetailDescriptor>
<dxg:DataControlDetailDescriptor ItemsSourceBinding="{Binding Items}">
<dxg:GridControl AutoGenerateColumns="AddNew" CurrentItem="{Binding Level2CurrentItem}">
<dxg:GridControl.DetailDescriptor>
<dxg:DataControlDetailDescriptor ItemsSourceBinding="{Binding Items}">
<dxg:GridControl AutoGenerateColumns="AddNew" CurrentItem="{Binding Level3CurrentItem}" />
</dxg:DataControlDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
</dxg:GridControl>
</dxg:DataControlDetailDescriptor>
</dxg:GridControl.DetailDescriptor>
</dxg:GridControl>
</DockPanel>
</Window>
C#using DevExpress.Mvvm;
using System.Collections.ObjectModel;
namespace MasterDetailInside {
public class ViewModel : BindableBase {
public Item Level1CurrentItem { get { return GetValue<Item>(); } set { SetValue(value); } }
public Item Level2CurrentItem { get { return GetValue<Item>(); } set { SetValue(value); } }
public Item Level3CurrentItem { get { return GetValue<Item>(); } set { SetValue(value); } }
public ObservableCollection<Item> Data { get; }
public ViewModel() {
Data = new ObservableCollection<Item>();
for (int i = 0; i < 50; i++) {
Item item1 = new Item() { Id = i, Name = string.Format("Item_{0}", i), };
for (int j = 0; j < 10; j++) {
Item item2 = new Item() { Id = j, Name = string.Format("Item_{0}.{1}", i, j) };
for (int k = 0; k < 5; k++) {
item2.Items.Add(new Item() { Id = k, Name = string.Format("Item_{0}.{1}.{2}", i, j, k) });
}
item1.Items.Add(item2);
}
Data.Add(item1);
}
}
}
public class Item : BindableBase {
public int Id { get { return GetValue<int>(); } set { SetValue(value); } }
public string Name { get { return GetValue<string>(); } set { SetValue(value); } }
public ObservableCollection<Item> Items { get; }
public Item() {
Items = new ObservableCollection<Item>();
}
}
}