WPF 依赖属性

来源:互联网 发布:数控平面钻的编程 编辑:程序博客网 时间:2024/06/06 05:47


WPF 的ListBox 可以绑定SelectedItem但是不可以绑定SelectedItems不能绑定,这就有点烦了。

所以可以自定义一个依赖属性来绑定:

首先定义个TListBox类来代替ListBox

    public class TListBox:ListBox
    {
        public static readonly DependencyProperty SelectItemsProperty;        
        public List<ListBoxData> SelectItems
        {
            get
            {
                return (List<ListBoxData>)GetValue(SelectItemsProperty);
            }
            set
            {
                SetValue(SelectItemsProperty, value);
            }
        }

        static TListBox()
        {
            SelectItemsProperty = DependencyProperty.Register("SelectItems", typeof(List<ListBoxData>), typeof(TListBox));//, data);
        }
        public TListBox()
        {
            this.SelectionChanged += TListBox_SelectionChanged;
        }
        private void TListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<ListBoxData> _items = new List<ListBoxData>();
            TListBox box = sender as TListBox;
            foreach(var itm in box.SelectedItems)
            {
                _items.Add(itm as ListBoxData);
            }
            SelectItems = _items;
        }
    }

创建ViewModel

    public class ViewModel:INotifyPropertyChanged
    {
        private string _text = "1234";
        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                _text = value;
                RaisePropertyChanged(nameof(Text));
            }
        }
        private List<ListBoxData> selectdatas = null;
        public List<ListBoxData> SelectDatas
        {
            get
            {
                return selectdatas;
            }
            set
            {
                selectdatas = value;
                RaisePropertyChanged(nameof(SelectDatas));
            }
        }
        private List<ListBoxData> datas = null;
        public List<ListBoxData> Datas
        {
            get
            {
                return datas;
            }
            set
            {
                datas = value;
                RaisePropertyChanged(nameof(Datas));


            }
        }
        public ViewModel()
        {
            datas = new List<ListBoxData>();
            datas.Add(new ListBoxData(0, "name1"));
            datas.Add(new ListBoxData(1, "name2"));
            datas.Add(new ListBoxData(2, "name3"));
            datas.Add(new ListBoxData(3, "name4"));
            datas.Add(new ListBoxData(4, "name5"));
            datas.Add(new ListBoxData(5, "name6"));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string name)
        {
            if(PropertyChanged!=null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }

    public class ListBoxData
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ListBoxData(int id ,string name)
        {
            Id = id;
            Name = name;
        }
    }


使用TListBox

    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="411,277,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
        <local:TListBox x:Name="listBox" ItemsSource="{Binding Path=Datas}" DisplayMemberPath="Name"
                        SelectItems="{Binding Path=SelectDatas,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"                        
                        HorizontalAlignment="Left" Height="188" Margin="38,22,0,0" VerticalAlignment="Top" Width="162" SelectionMode="Extended"/>
    </Grid>

在Button的Click里验证:

        private void button_Click(object sender, RoutedEventArgs e)
        {
            string info = null;
            foreach (var v in vm.SelectDatas)
            {
                info += v.Name + "\n";
            }
            MessageBox.Show(info);
        }

好像没毛病