[英]Relative binding in style of control inside ListBoxItem template
My problem is with the following code, with binding the IsAvailable
property of the MyListBoxItem
class. 我的问题是以下代码与MyListBoxItem
类的IsAvailable
属性绑定IsAvailable
。 My current solution: 我当前的解决方案:
<ListBox ItemTemplate="{StaticResource myTemplate}">
<ListBox.Resources>
<DataTemplate x:Key="myTemplate" DataType="{x:Type local:MyListBoxItem}">
<Label Foreground="Green" Content="{Binding Title}" Tag="{Binding IsAvailable}">
<Label.Style>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</DataTemplate>
... (more datatemplates)
</ListBox.Resources>
</ListBox>
My question: In my solution the value of IsAvailable
"goes through" two bindings. 我的问题:在我的解决方案中, IsAvailable
的值“通过”两个绑定。 The first one binds the value to the Tag
property of the Label
and then in the style triggers, a trigger checks its value and sets a property of the Label
. 第一个将值绑定到Label
的Tag
属性,然后在样式触发器中,触发器检查其值并设置Label
的属性。 When I used Binding="{Binding IsAvailable, RelativeSource={RelativeSource AncestorType={x:Type local:MyListBoxItem}}}"
it didn't work, because the Style
can't see any ancestor of the Label
(or something similar reason), it resulted binding errors (with code 4 or 40 maybe), for each item added to the ListBox
. 当我使用Binding="{Binding IsAvailable, RelativeSource={RelativeSource AncestorType={x:Type local:MyListBoxItem}}}"
它无效,因为Style
无法看到Label
任何祖先(或类似原因) ),则对于添加到ListBox
每个项目都会导致绑定错误(可能是代码4或40)。
So finally: can I make the solution more simple, or there is no another (better) one? 所以最后:我可以使解决方案更简单,还是没有其他(更好的)解决方案?
An important thing I've forgot to mention, sorry : I put the DataTemplate
in the ListBox's resources because I have more templates (they are basically differ, so I can't style them with triggers), which I have to switch between sometimes... 抱歉 ,我忘了提到一个重要的事情:我将DataTemplate
放在ListBox的资源中,因为我有更多的模板(它们基本上是不同的,所以我不能用触发器设置样式),有时不得不在它们之间进行切换。 ..
The ItemTemplate
will take the type that the ItemsSource
is bound to. ItemTemplate
将采用ItemsSource
绑定到的类型。 Therefore you should be able to simply bind to IsAvailable
, as the ListBox's item type is MyListBoxItem
. 因此,您应该能够简单地绑定到IsAvailable
,因为ListBox的项目类型是MyListBoxItem
。 Try this: 尝试这个:
<ListBox ItemsSource="...">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Foreground="Green" Content="{Binding Title}" Tag="{Binding IsAvailable}">
<Label.Style>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource Self}}" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You'll need to set your ItemsSource
property to a Binding
to the MyListBoxItem
collection. 您需要将ItemsSource
属性设置为Binding
到MyListBoxItem
集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.