Example E4402
Visible to All Users

WPF Data Grid - Bind Master and Detail Focused Rows to View Model Properties

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:

image

You can bind CurrentItem, SelectedItem, and SelectedItems properties for master and detail grids to View Model properties.

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Window1.xaml
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>
ViewModel.cs(vb)
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>(); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.