WPF 依赖属性


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

好像没毛病

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值