如何在不覆盖Mahapps主题的情况下设置属性? [英] How to set attributes without overriding the Mahapps theme?

查看:193
本文介绍了如何在不覆盖Mahapps主题的情况下设置属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Mahapps用于GUI,但是我想设置一些与视觉属性不同的属性,例如marginsverticalAlignment,因此我将其添加到了UserControl.resources部分

I'm using Mahapps for a GUI, however I want to set some attributes different than visual ones such as margins and verticalAlignment, so I added this to the UserControl.resources section

<Style x:Key="{x:Type TextBox}" TargetType="TextBox" BasedOn="{StaticResource ResourceKey={x:Type TextBox}}">
     <Setter Property="Margin" Value="2"/>
     <Setter Property="VerticalAlignment" Value="Center"/>
</Style> 

但是它会覆盖TextBoxes的所有视觉样式属性,我如何才能添加这些属性而不覆盖所有视觉样式设置?

However it overrides all the visual styles attributes of the TextBoxes, how can I just add those attributes without overriding all the visual styles settings?

推荐答案

给样式一个键

<Style x:Key="myCustomTextBoxStyle"
       TargetType="TextBox"
       BasedOn="{StaticResource ResourceKey={x:Type TextBox}}">
  <Setter Property="Margin" Value="2"/>
  <Setter Property="VerticalAlignment" Value="Center"/>
</Style>

并在需要的地方使用它

<TextBox Style={StaticResource myCustomTextBoxStyle} />

编辑 或将其放入用户控件或窗口资源的主要资源字典中而无需使用键

EDIT or put it to the main resource dictionary of user control or window resource without a key

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>

        </ResourceDictionary.MergedDictionaries>

        <Style TargetType="TextBox"
               BasedOn="{StaticResource ResourceKey={x:Type TextBox}}">
          <Setter Property="Margin" Value="2"/>
          <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>
    </ResourceDictionary>
</Window.Resources>

希望有帮助

这篇关于如何在不覆盖Mahapps主题的情况下设置属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆