开发者

Styling Visual States

I am using the VisualStateManager from the WPF Toolkit. I've created a custom control, part of a reusable controls library, with numerous visual states. Now I want to allow the client of my library to easily restyle the fonts and colors of these visual states. What's the standard way of doing this? Must I require the client to replace the entire control template and replace all the visual sta开发者_运维百科tes, even if they're only interested in modifying just one? Or is there a better way... like how could I make GoToState defer to a client-supplied trigger intended to override a visual state's default font and color? Other ideas?


I got this working for the first case I tried. The goal was to make it so that this code would have an effect:

  <l:MyControl>
    <l:MyControl.MyStateStyle>
      <Style>
        <Setter Property="Control.Background" Value="LightBlue"/>
        <Setter Property="TextElement.Foreground" Value="White"/>
        <Setter Property="TextElement.FontStyle" Value="Italic"/>
      </Style>
    </l:MyControl.MyStateStyle> </l:MyControl> 

This is how I did it:

Style style = new Style();

if ( MyState == MyState.State1Normal )
    VisualStates.GoToState( this, useTransitions, State1Normal );
else if ( MyState == MyState.State2 ) {
    if ( Owner.State2Style != null )
        style = style.Merge( Owner.State2Style );
    else
        VisualStates.GoToState( this, useTransitions, State2 );
} else if ( MyState == MyState.State3 ) {
    if ( Owner.State3Style != null )
        style = style.Merge( Owner.State3Style );
    else
        VisualStates.GoToState( this, useTransitions, State3 );
}

Style = style;

Notice the Style.Merge extension method. I got it from http://bea.stollnitz.com/blog/?p=384 . It enables me to combine the effects of multiple visual state groups.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜