Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top