How to set attributes without overriding the Mahap

2019-09-01 03:28发布

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条回答
我想做一个坏孩纸
2楼-- · 2019-09-01 04:00

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

查看更多
登录 后发表回答