Yisela
Yisela

Reputation: 6941

Why is my XAML control losing properties when applying a style template to it?

I'm trying to style this button, and I even though I state the height it should be in my XAML, the template in the styling seems to get rid of it.

Note: I am aware I can just have a style with no template, but I need the template because I'm using multiple themes / style files.

Control:

<Button Style="{DynamicResource basicButton}"
        Height="50">
    <Canvas...>
        <Path... />
    </Canvas>
</Button>

Styling:

<Style x:Key="basicButton" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{DynamicResource solid_single_mainColor}">
                    <ContentPresenter />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Is this expected behavior? I intent to re-use this button style, so I need to be able to give it a height in the XAML. What am I missing? How can I give each new button I create a different height?

Upvotes: 2

Views: 214

Answers (1)

John Bowen
John Bowen

Reputation: 24453

The problem is likely coming from the Canvas that you are placing inside the Button. In many cases a Canvas will have unbounded size unless you explicitly clip it. The Height of the Button itself isn't affected by its template as layout measurement will use the explicit value you have set but you may not be able to tell visually what size the Button itself is.

Upvotes: 1

Related Questions