Articles in this section
Category / Section

How to bind the selected item and current item in WPF DataGrid ?

2 mins read

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.


Conclusion

I hope you enjoyed learning about how to bind the selected item and current item in the DetailsViewDataGrid of WPF DataGrid (SfDataGrid).

You can refer to our WPF DataGrid feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF DataGrid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied