How can I both a Style and a Template to a Button?

Stout 286 Reputation points
2021-02-26T19:33:00.937+00:00

Hi. How can I both a Style and a Template to a Button?

I have a Style with a DataTrigger that sets the background color of the Button to either Gray (if disabled) or Blue (if enabled).

I also have a ControlTemplate that sets the CornerRadius to give it some rounded corners.

Each of those two works individually. But when I add them both to the Button, the ControlTemplate takes precedence, and the Style with the DataTrigger is lost. I end up seeing a button with no border, and no background.

How can I accomplish both of these tasks together?

Thank you.

    <Button Grid.Row="4" Command="{Binding SubmitCommand}"
            IsEnabled="{Binding IsSubmitButtonEnabled.Value}"
            HorizontalAlignment="Stretch" VerticalAlignment="Center" FontSize="18px" Height="36">

        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Background" Value="Gray" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSubmitButtonEnabled.Value}" Value="True">
                        <Setter Property="Background" Value="Blue" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>

        <Button.Template>
            <ControlTemplate>
                <Border CornerRadius="20">
                    <TextBlock Text="Submit"
                               HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               TextAlignment="Center"
                               FontSize="16">
                    </TextBlock>
                </Border>
            </ControlTemplate>
        </Button.Template>

    </Button>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2021-02-27T06:22:11.003+00:00

    Hi,
    use TemplateBinding in ControlTemplate like this:

    <Button Grid.Row="4" Command="{Binding SubmitCommand}"  
             IsEnabled="{Binding IsSubmitButtonEnabled.Value}"  
             HorizontalAlignment="Stretch" VerticalAlignment="Center" FontSize="18px" Height="36">  
      <Button.Style>  
        <Style TargetType="{x:Type Button}">  
          <Setter Property="Background" Value="Gray" />  
          <Style.Triggers>  
            <DataTrigger Binding="{Binding IsSubmitButtonEnabled.Value}" Value="True">  
              <Setter Property="Background" Value="Blue" />  
            </DataTrigger>  
          </Style.Triggers>  
        </Style>  
      </Button.Style>  
      <Button.Template>  
        <ControlTemplate>  
          <Border CornerRadius="20" Background="{TemplateBinding Background}">  
            <TextBlock Text="Submit"  
                       HorizontalAlignment="Center"  
                       VerticalAlignment="Center"  
                       TextAlignment="Center"  
                       FontSize="16">  
            </TextBlock>  
          </Border>  
        </ControlTemplate>  
      </Button.Template>  
    </Button>  
    

    Result:

    72652-x.gif

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful