视觉状态造型

发布于 2024-08-13 10:21:26 字数 245 浏览 4 评论 0原文

我正在使用 WPF 工具包中的 VisualStateManager。我创建了一个自定义控件,它是可重用控件库的一部分,具有多种视觉状态。现在我想允许我的库的客户轻松地重新设计这些视觉状态的字体和颜色。这样做的标准方法是什么?我是否必须要求客户替换整个控件模板并替换所有视觉状态,即使他们只对修改一个感兴趣?或者是否有更好的方法...比如我如何使 GoToState 遵循客户端提供的触发器,旨在覆盖视觉状态的默认字体和颜色?还有其他想法吗?

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 states, 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯 2024-08-20 10:21:26

我在尝试的第一个案例中就成功了。目标是使此代码产生效果:

  <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> 

这就是我的做法:

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;

注意 Style.Merge 扩展方法。我从 http://bea.stollnitz.com/blog/?p=384< /a> .它使我能够结合多个视觉状态组的效果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文