Friday, November 16, 2012

WPF - Inheriting a style



In the following style, a TextBlock's style in created with blinking animation.
// Here is the base style for TextBlock
        <Style x:Key="CellBaseStyle"  TargetType="{x:Type TextBlock}">
            <Setter Property="Background" Value="Transparent"/> 
            <Setter Property="Padding" Value="0,50,0,0"></Setter>
        </Style>

// Here you will use BasedOn property of Style to use the base style for TextBlock, properties in base style can be overridden in this style as well just like background property
        <Style TargetType="{x:Type TextBlock}" x:Key="SelectedCellStyle" 
               BasedOn="{StaticResource CellBaseStyle}">
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush>
                        <SolidColorBrush.Color>
                            
                            <Color R="0" G="168" B="0"/>
                        </SolidColorBrush.Color>
                    </SolidColorBrush>
                </Setter.Value>
            </Setter>           
 
            <Setter Property="Foreground" Value="White"/>
            
            <Style.Triggers>
                <EventTrigger RoutedEvent="Binding.TargetUpdated">
                    <BeginStoryboard HandoffBehavior="Compose">
                        <Storyboard TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)">
                            <ColorAnimation Duration="0:0:0.7" To="DarkGreen"   AutoReverse="True" RepeatBehavior="1" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>

No comments:

Post a Comment