将 ListBoxItem 的 IsSelected 属性绑定到对象的源属性 [英] Binding the IsSelected property of ListBoxItem to a property on the object from it's source

查看:29
本文介绍了将 ListBoxItem 的 IsSelected 属性绑定到对象的源属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF ListBox 控件,我将它的 ItemsSource 设置为项目对象的集合.如何将 ListBoxItemIsSelected 属性绑定到相应项目对象的 Selected 属性,而无需将该对象的实例设置为一个 Binding.Source?

I have a WPF ListBox control and I'm setting its ItemsSource to a collection of item objects. How can I bind the IsSelected property of the ListBoxItem to a Selected property of a corresponding item object without having an instance of the object to set as a Binding.Source?

推荐答案

只需覆盖 ItemContainerStyle:

Just override ItemContainerStyle:

   <ListBox ItemsSource="...">
     <ListBox.ItemContainerStyle>
      <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="IsSelected" Value="{Binding Selected}"/>
      </Style>
     </ListBox.ItemContainerStyle>
    </ListBox>

哦,顺便说一句,我想你会喜欢 dr.WPF 的这篇精彩文章:ItemsControl:A 到 Z.

Oh, by the way, I think you'd like this wonderful articles from dr.WPF: ItemsControl: A to Z.

希望这会有所帮助.

这篇关于将 ListBoxItem 的 IsSelected 属性绑定到对象的源属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆