将 ListBoxItem 的 IsSelected 属性绑定到对象源中的属性

发布于 2024-08-13 20:58:32 字数 215 浏览 2 评论 0原文

我有一个 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白日梦 2024-08-20 20:58:32

只需覆盖 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

希望这有帮助。

Just override ItemContainerStyle:

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

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

Hope this helps.

病毒体 2024-08-20 20:58:32

我一直在寻找代码解决方案,所以这里是它的翻译。

System.Windows.Controls.ListBox innerListBox = new System.Windows.Controls.ListBox();

//The source is a collection of my item objects.
innerListBox.ItemsSource = this.Manager.ItemManagers;

//Create a binding that we will add to a setter
System.Windows.Data.Binding binding = new System.Windows.Data.Binding();
//The path to the property on your object
binding.Path = new System.Windows.PropertyPath("Selected"); 
//I was in need of two way binding
binding.Mode = System.Windows.Data.BindingMode.TwoWay;

//Create a setter that we will add to a style
System.Windows.Setter setter = new System.Windows.Setter();
//The IsSelected DP is the property of interest on the ListBoxItem
setter.Property = System.Windows.Controls.ListBoxItem.IsSelectedProperty;
setter.Value = binding;

//Create a style
System.Windows.Style style = new System.Windows.Style();
style.TargetType = typeof(System.Windows.Controls.ListBoxItem);
style.Setters.Add(setter);

//Overwrite the current ItemContainerStyle of the ListBox with the new style 
innerListBox.ItemContainerStyle = style;

I was looking for a solution in code, so here is the translation of that.

System.Windows.Controls.ListBox innerListBox = new System.Windows.Controls.ListBox();

//The source is a collection of my item objects.
innerListBox.ItemsSource = this.Manager.ItemManagers;

//Create a binding that we will add to a setter
System.Windows.Data.Binding binding = new System.Windows.Data.Binding();
//The path to the property on your object
binding.Path = new System.Windows.PropertyPath("Selected"); 
//I was in need of two way binding
binding.Mode = System.Windows.Data.BindingMode.TwoWay;

//Create a setter that we will add to a style
System.Windows.Setter setter = new System.Windows.Setter();
//The IsSelected DP is the property of interest on the ListBoxItem
setter.Property = System.Windows.Controls.ListBoxItem.IsSelectedProperty;
setter.Value = binding;

//Create a style
System.Windows.Style style = new System.Windows.Style();
style.TargetType = typeof(System.Windows.Controls.ListBoxItem);
style.Setters.Add(setter);

//Overwrite the current ItemContainerStyle of the ListBox with the new style 
innerListBox.ItemContainerStyle = style;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文