我有一個類繼承ListBox和ListBoxItems自定義ControlTemplate。 如果條件爲真,我想更改ListBoxItems背景。我試圖爲此使用DataTrigger。我不想檢查ListBoxItems上下文對象中的條件,我想在繼承的ListBox類中檢查它。自定義模板listboxitem觸發器綁定到列表框
問題是如何在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;
}
}