We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to bind the selecteditem and currentitem in DetailsViewDataGrid of WPF DataGrid (SfDataGrid)?

Platform: WPF |
Control: SfDataGrid |
.NET Framework: 4.5

You can bind the SelectedItem and CurrentItem properties directly in WPF DataGrid (SfDataGrid). But in Master DetailsView you can’t be able to bind the SelectedItem or CurrentItem directly in DetailsViewDataGrid. You can overcome this by using behavior with dependency properties which is bounded to ViewModel properties. You have to use SelectionChanged event of corresponding DetailsViewDataGrid to get the SelectedItem and CurrentItem of WPF DataGrid (SfDataGrid).

C#

public class SelectionBehaviour : Behavior<SfDataGrid>
{
    public object DetailsViewSelectedItem
    {
        get { return (object)GetValue(DetailsViewSelectedItemProperty); }
        set { SetValue(DetailsViewSelectedItemProperty, value); }
    }
 
    public static readonly DependencyProperty DetailsViewSelectedItemProperty =
        DependencyProperty.Register("DetailsViewSelectedItem", typeof(object), typeof(SelectionBehaviour), new PropertyMetadata(null));
 
    public object DetailsViewCurrentItem
    {
        get { return (object)GetValue(DetailsViewCurrentItemProperty); }
        set { SetValue(DetailsViewCurrentItemProperty, value); }
    }
    public static readonly DependencyProperty DetailsViewCurrentItemProperty =
        DependencyProperty.Register("DetailsViewCurrentItem", typeof(object), typeof(SelectionBehaviour), new PropertyMetadata(null));
 
    public ObservableCollection<object> DetailsViewSelectedItems
    {
        get { return (ObservableCollection<object>)GetValue(DetailsViewSelectedItemsProperty); }
        set { SetValue(DetailsViewSelectedItemsProperty, value); }
    }
 
    public static readonly DependencyProperty DetailsViewSelectedItemsProperty =
        DependencyProperty.Register("DetailsViewSelectedItems", typeof(ObservableCollection<object>), typeof(SelectionBehaviour), new PropertyMetadata(null));
 
    protected override void OnAttached()
    {           
        this.AssociatedObject.Loaded += AssociatedObject_Loaded;
    }
 
    void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        (this.AssociatedObject.DetailsViewDefinition[0] as GridViewDefinition).DataGrid.SelectionChanged += FirstDetailsViewDataGridSelectionChanged;          
    }
    public Object ViewCurrentItem
    {
        get { return (Object)GetValue(ViewCurrentItemProperty); }
        set { SetValue(ViewCurrentItemProperty, value); }
    }
 
    public static readonly DependencyProperty ViewCurrentItemProperty =
        DependencyProperty.Register("ViewCurrentItem", typeof(Object), typeof(SelectionBehaviour), new PropertyMetadata(null));
       
    void FirstDetailsViewDataGridSelectionChanged(object sender, GridSelectionChangedEventArgs e)
    {
        this.DetailsViewSelectedItem = (e.OriginalSender as SfDataGrid).SelectedItem;
        this.DetailsViewSelectedItems = (e.OriginalSender as SfDataGrid).SelectedItems;
        this.DetailsViewCurrentItem = (e.OriginalSender as SfDataGrid).CurrentItem;
    }
    protected override void OnDetaching()
    {
        this.AssociatedObject.Loaded -= AssociatedObject_Loaded;
    }
}

XAML

<syncfusion:SfDataGrid Name="datagrid"
                               Grid.Column="0"
                               AutoGenerateColumns="False"
                               ItemsSource="{Binding OrderInfoCollection}"
                               NavigationMode="Cell">
            <interactivity:Interaction.Behaviors>
                <local:SelectionBehaviour DetailsViewCurrentItem="{Binding FirstNestedCurrentDetails,
                                                                           Mode=TwoWay}"
                                          DetailsViewSelectedItem="{Binding SelectedFirstNestedGridOrderDetails,
                                                                            Mode=TwoWay}"
                                          DetailsViewSelectedItems="{Binding FirstNestedGridSelectedItems,
                                                                             Mode=TwoWay}" />
            </interactivity:Interaction.Behaviors>
 
            <syncfusion:SfDataGrid.Columns>
                <syncfusion:GridTextColumn HeaderText="Order ID" MappingName="OrderID" />
                <syncfusion:GridTextColumn HeaderText="Customer ID" MappingName="CustomerID" />
                <syncfusion:GridTextColumn HeaderText="Customer Name" MappingName="CustomerName" />
                <syncfusion:GridTextColumn HeaderText="Country" MappingName="Country" />
                <syncfusion:GridTextColumn HeaderText="Ship City" MappingName="ShipCity" />
            </syncfusion:SfDataGrid.Columns>
            <syncfusion:SfDataGrid.DetailsViewDefinition>
                <syncfusion:GridViewDefinition RelationalColumn="ProductDetails">
                    <syncfusion:GridViewDefinition.DataGrid>
                        <syncfusion:SfDataGrid x:Name="DetailsViewGrid"
                                               AllowEditing="True"
                                               AutoGenerateColumns="False"
                                               NavigationMode="Cell">
                            <syncfusion:SfDataGrid.Columns>
                                <syncfusion:GridTextColumn HeaderText="Order ID" MappingName="OrderID" />
                                <syncfusion:GridTextColumn HeaderText="Product Name" MappingName="ProductName" />
                            </syncfusion:SfDataGrid.Columns>
                        </syncfusion:SfDataGrid>
                    </syncfusion:GridViewDefinition.DataGrid>
                </syncfusion:GridViewDefinition>
            </syncfusion:SfDataGrid.DetailsViewDefinition>
</syncfusion:SfDataGrid>

View sample in GitHub.

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Brent Hoskisson
Apr 09, 2019

What is "interactivity:..."

Please include the definition of the namespace here.

Thanks.

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile