本文介绍了Data Grid SelectedItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要数据绑定数据网格的选定项目。但没有数据网格选定项目的依赖属性。

如何绑定所选项目

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

推荐答案

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));
        }
    }





您现在可以将您的控件用作普通的GridView控件,并进行更改。 br />



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!


这篇关于Data Grid SelectedItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 16:40