Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Silverlight
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 19:29pm
Comments
Christian Amado at 20-Nov-12 12:25pm
   
Bind data with what control? Can you clarify your question?, please

1 solution

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

Solution 1

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:
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.
<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!
  Permalink  

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

  Print Answers RSS
0 Nirav Prabtani 284
1 OriginalGriff 217
2 Sergey Alexandrovich Kryukov 196
3 Mika Wendelius 185
4 CPallini 170
0 Nirav Prabtani 284
1 OriginalGriff 217
2 Sergey Alexandrovich Kryukov 196
3 Mika Wendelius 185
4 CPallini 170


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2014
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