Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
Christian Amado 20-Nov-12 12:25pm    
Bind data with what control? Can you clarify your question?, please

1 solution

Hello
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.

Example:
C#
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); }
            set
            {
                SetValue(SelectedItemsProperty, value);
                NotifyPropertyChanged("SelectedItems");
            }
        }

        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.
HTML
<grid>
    <local:mydatagrid selectionmode="Extended" xmlns:local="#unknown">
                      ItemsSource="{Binding Path=Items, Mode=OneWay}"
                      SelectedItems="{Binding Path=SelectedItems, Mode=TwoWay}"
                      Margin="0,0,0,20"/>
    <textblock text="{Binding Path=SelectedItems.Count, Mode=OneWay}">
               VerticalAlignment="Bottom" />
</textblock></local:mydatagrid></grid>


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

Hope it helps!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900