Click here to Skip to main content
13,409,655 members (41,366 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want To data bind the selected items of data grid.But no dependency property of selected items for data grid.
How can I do the selected items binding
Posted 6-Nov-12 20:29pm
Christian Amado 20-Nov-12 12:25pm
Bind data with what control? Can you clarify your question?, please

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I had a similar problem, and I solved this by creating a control that inherits the GridView control (let's call it MyGridView), where I have a new property (with dependency property) that holds the selected items. Then I attach to the SelectionChanged and set the new SelectedItems-property.

public class MyDataGrid : DataGrid, INotifyPropertyChanged
        public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItems", typeof(IList), typeof(MyDataGrid), new PropertyMetadata(null));
        public new IList SelectedItems
            get { return (IList)GetValue(SelectedItemsProperty); }
                SetValue(SelectedItemsProperty, value);

        public MyDataGrid()
            : base()
            base.SelectionChanged += new SelectionChangedEventHandler(MyDataGrid_SelectionChanged);

        private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
            this.SelectedItems = base.SelectedItems;

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String aPropertyName)
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(aPropertyName));

You can now use your control as an ordinary GridView control, with your alterations.
    <local:mydatagrid selectionmode="Extended" xmlns:local="#unknown">
                      ItemsSource="{Binding Path=Items, Mode=OneWay}"
                      SelectedItems="{Binding Path=SelectedItems, Mode=TwoWay}"
    <textblock text="{Binding Path=SelectedItems.Count, Mode=OneWay}">
               VerticalAlignment="Bottom" />

You can put more specific code into this control, like navigation behaviors, styling and new functionality.

Hope it helps!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180221.1 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100