风格触发器导致控制失去主题

Style Triggers Cause Control To Lose Theme

我有一个 WPF 应用程序,它使用 MahApps Metro 作为 UI 主题。我还需要使用样式触发器,以便我可以根据 属性 适当地确定控件是否可见。触发器有效,但具有删除主题的副作用。所以现在它看起来像一个默认的 WPF unthemed CheckBox。有什么方法可以在 CheckBox 上保留 MahApps Metro 主题?

分配样式时,您会覆盖默认样式或之前应用的任何样式。如果你想扩展一个样式,你已经使用 BasedOn 属性.

指定了基本样式

Styles can be based on other styles through this property. When you use this property, the new style will inherit the values of the original style that are not explicitly redefined in the new style.

  • 指定控件类型使您的样式基于控件的隐式样式。

    <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}">
       <!-- ...your setters and triggers. -->
    </Style>
    
  • 指定您想要作为样式基础的样式键。

    <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource MyCheckBoxBaseStyle}">
       <!-- ...your setters and triggers. -->
    </Style>
    

    MahApps 中 CheckBox 的命名样式可以在 here on GitHub.

    中找到

请注意,虽然您的 Visibility 触发器应该可以工作,但 CheckBox 样式的控件模板 中定义的其他触发器优先您将无法以自己的风格重新定义它们。如果遇到这种情况,您必须将相应的样式从 GitHub 复制到您的项目中并根据您的要求进行调整。