Below i have two control templates that are used as cell templates for two different columns in a grid. You’ll notice that both columns are bound to the same model properties (Code and Value), but use a converter to display those values differently. Both control template also use the same Style to ‘blink’ the cell when data is changed.
this works, but not exactly the way i want it. Right now, when either Data.Code or Data.Value changes, BOTH column cells Blink. What i want is if Data.Code == “CodeA”, then the column using template CDisplay2 should not blink (infact, it should not display anything). And if Data.Code == “CodeB”, then the cell using template CDisplay1 should not blink.
To achieve this it would be great if i could conditionally apply the style template based on Data.Code, but i can’t figure out how to do that. Anythoughts on this? How can i selectively apply a style to a multiple controls bound to same model property based on a particular property value?
<Style x:Key="FlashStyle" TargetType="TextBlock" >
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames RepeatBehavior="4x" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
<DiscreteObjectKeyFrame KeyTime="0:0:0.4" Value="{x:Static Visibility.Collapsed}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
<ControlTemplate x:Key="CDisplay1" >
<Grid>
<TextBlock Style="{StaticResource Flash1}" >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource conv}" ConverterParameter="CodeA" NotifyOnTargetUpdated="True">
<Binding Path="Data.Code" />
<Binding Path="Data.Value" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="CDisplay2" >
<Grid>
<TextBlock Style="{StaticResource Flash1}" >
<TextBlock.Text>
<MultiBinding Converter="{StaticResource conv}" ConverterParameter="CodeB" NotifyOnTargetUpdated="True">
<Binding Path="Data.Code" />
<Binding Path="Data.Value" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</ControlTemplate>
I used a trigger to set the binding conditionally. this effect of this is that FlashStyle won’t execute for both controls since some cells won’t be bound and the converter won’t be invoked.