【发布时间】:2011-10-03 11:20:06
【问题描述】:
我有一个继承自 ListBox 的类和一个用于 ListBoxItems 的自定义 ControlTemplate。 如果条件为真,我想更改 ListBoxItems 背景。我试图为此使用 DataTrigger。我不想检查 ListBoxItems 上下文对象中的条件,我想在继承的 ListBox 类中检查它。
问题是如何在 ControlTemplate 中将 Trigger 绑定到 ListBox 属性,当它需要在运行时为每个 ListBoxItem 确定正确的值时?
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="bd">
<TextBlock Name="lbl" Text="{Binding Path=DataChar}" FontWeight="ExtraBold" FontSize="15" Margin="5"/>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={ RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}, Path=IsSymbolExists}" Value="True">
<Setter TargetName="bd" Property="Background" Value="Yellow" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
public class CustomListBox : ListBox
{
...
public bool IsSymbolExists
{
if(/*condition is true for the ListBoxItem*/)
return true;
return false;
}
}
【问题讨论】:
标签: wpf templates triggers listbox