Apply Style based on property value
Working with the Infragistics XamDataGrid I encountered a situation where I want a Style applied only if a certain property is set. However, I think this is more of a general WPF/style question than xamDataGrid specific.
The below style is what I am currently using. It adds checkBoxes to the record selector area:
<Style TargetType="{x:Type igDP:RecordSelector}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igDP:RecordSelector}">
<CheckBox x:Name="HeaderCheckBox"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsChecked="{Binding Path=DataItem.IsChecked}">
</CheckBox>
<ControlTemplate.Triggers>
<Trigger Property="IsFilterRecord" Value="True">
<Setter TargetName="HeaderCheckBox" Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsAddRecord" Value="True">
<Setter TargetName="HeaderCheckBox" Property="Visibility" Value="Collapsed"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The TargetType is R开发者_StackOverflow中文版ecordSelector. If the record is either the filter row or the add record row, I don't want to show the check box.
I want to change this so that if the record is the add record row (IsAddRecord == true), then do not apply the style at all. I want the add record row to retain its default style.
Is this possible?
You can't prevent the Style from being aplied from within the Style itself, but you can prevent its Setters from being applied using Style.Triggers:
<Style TargetType="{x:Type igDP:RecordSelector}">
<Style.Triggers>
<Trigger Property="IsAddRecord" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igDP:RecordSelector}">
<CheckBox x:Name="HeaderCheckBox"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsChecked="{Binding Path=DataItem.IsChecked}">
</CheckBox>
<ControlTemplate.Triggers>
<Trigger Property="IsFilterRecord" Value="True">
<Setter TargetName="HeaderCheckBox" Property="Visibility" Value="Collapsed"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
精彩评论