我需要做的是将ListBox绑定到数据项类的可观察集合,这很容易,但实质上,将每个ListBoxItem的IsSelected状态绑定到相应数据项中的布尔属性.
并且,我需要它是双向的,以便我可以使用viewmodel中的选定和未选定项填充ListBox.
我看了很多实现,但没有一个适合我.他们包括:
>将DataTrigger添加到ListBoxItem的样式并调用状态 *** 作更改
我意识到这可以通过事件处理程序在代码隐藏中完成,但考虑到域的复杂性,它将非常混乱.我宁愿坚持使用viewmodel进行双向绑定.
谢谢.
标记
public class Item : INotifyPropertyChanged{ // INotifyPropertyChanged stuff not shown here for brevity public string ItemText { get; set; } public bool IsItemSelected { get; set; }}public class viewmodel : INotifyPropertyChanged{ public viewmodel() { Items = new ObservableCollection<Item>(); } // INotifyPropertyChanged stuff not shown here for brevity public ObservableCollection<Item> Items { get; set; }}
<ListBox ItemsSource="{Binding Items,Source={StaticResource viewmodel}}" SelectionMode="Extended"> <ListBox.ItemContainerStyle> <Style targettype="ListBoxItem"> <Setter Property="IsSelected" Value="{Binding IsItemSelected}"/> </Style> </ListBox.ItemContainerStyle> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding ItemText}"/> </DataTemplate> </ListBox.ItemTemplate></ListBox>总结
以上是内存溢出为你收集整理的wpf – 如何将ListBoxItem.IsSelected绑定到布尔数据属性全部内容,希望文章能够帮你解决wpf – 如何将ListBoxItem.IsSelected绑定到布尔数据属性所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)