How to set attributes without overriding the Mahapps theme?

拜拜、爱过 提交于 2019-12-12 00:27:10

问题


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> 

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?


回答1:


give the style a key

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

and use it where you need it

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

hope that helps



来源:https://stackoverflow.com/questions/20229898/how-to-set-attributes-without-overriding-the-mahapps-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!