Search code examples
c#wpfmaterial-design-in-xaml

Set height property of button style in material design in XAML to default


I am using MaterialDesign in XAML framework to style my WPF application.

At one place in my application I want to use a button which is styled via MaterialDesignPaperButton.

Because I slightly want to adjust this style, I derived a custom style:

<Style x:Key="StateMachineButton" TargetType="{x:Type Button}" 
       BasedOn="{StaticResource MaterialDesignPaperButton}">
    <Setter Property="Margin" Value="16 0 16 0" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <Setter Property="VerticalContentAlignment" Value="Stretch" />
    <Setter Property="HorizontalAlignment" Value="Center" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>

and later on set the button style to the defined one:

<Button Style="{StaticResource StateMachineButton}">
    <TextBlock Text="Long defined text here which shall be wraped." 
               TextWrapping="Wrap" />
</Button>

As you see, the Text on the button will be wrapped because it can be longer. For wrapping, the Height Property shall not be set.

But this height property is set in the used style. See this link.

Is there a way to reset the height property to its default value, like when it is not set?

I have already tried:

<Setter Property="Height" Value="{x:Null}" />

But the type double does not support null values.


Solution

  • The default value of the Width and Height properties is Double.NaN.

    In order to set that value, declare the following Setter, where the string NaN is automatically converted to Double.NaN.

    <Setter Property="Height" Value="NaN"/>
    

    You may also set the value explicitly. Add the following XAML namespace reference

    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    

    and declare this Setter:

    <Setter Property="Height" Value="{x:Static sys:Double.NaN}"/>