I have a class inherited from ListBox and a custom ControlTemplate for the ListBoxItems. I want to change the ListBoxItems background if a condition is true. I tried to use DataTrigger for this. I don't want to check the condition in the ListBoxItems context object, I want to check it in the inherited ListBox class.
The question is how can I bind in the ControlTemplate the Trigger to a ListBox property, when it needs to decide the right value for each ListBoxItem in runtime?
<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;
}
}