1

I have a quick question. I have implemented style for button. And it basically works. Here is the code (complete example: you can copy and paste, it will work):

<Window x:Class="TestWPFApplication.Window5"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestWPFApplication"
        mc:Ignorable="d"
        Title="Window5" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type Button}" x:Key="AButton">
            <Setter Property="Width" Value="120"></Setter>
            <Setter Property="Height" Value="50"></Setter>
            <Setter Property="Background" Value="DarkGreen" />
            <Setter Property="Foreground" Value="LightGreen" />
            <Setter Property="FontSize" Value="22" />
            <Setter Property="FontWeight" Value="Light" />
            <Setter Property="SnapsToDevicePixels" Value="True" />

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border CornerRadius="15" Background="{TemplateBinding Background}">
                            <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" 
                                              HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Button x:Name="QuitButton" Style="{StaticResource AButton}" Content="Quit" />
    </Grid>
</Window>

It's all fine. Button looks like this:

enter image description here

BUT If I move slightly the button, and the Margin property will be auto-generated, to this...

<Button x:Name="QuitButton" Style="{StaticResource AButton}" Content="Quit" Margin="88,114,87,108.5" />

.. button will look like this:

enter image description here

Right and bottom side of the button has been cut off. Don't know why:/ The question is: Can anyone explain me this? Thanks in advance!

3
  • try to remove Margin="0,0,0,0" from ContentPresenter and see if that helps you Commented Jan 13, 2016 at 1:44
  • @MohitShrivastava Thanks for interest, but it didn't help :/ Commented Jan 13, 2016 at 1:46
  • 1
    Don't ever waste your time using the form designer to position things. It always screws it up. I think the problem is that the Grid containing the button hasn't been enlarged to make room for the button at its new position. Remove the broken Margin value it added on the Button, and position it by other means. Grid row/column, StackPanel, WrapPanel, HorizontalAlignment, etc. Commented Jan 13, 2016 at 2:23

1 Answer 1

1

You've set the top margin to 114 and the bottom margin to 108.5 for a total of 225.5, but you've also set the total Window height to 300. That leaves just 77.5 pixels for the caption bar, the top and bottom window borders and the button (which you've set to 120 pixels high). The only way for WPF to make everything fit is to crop the button. (The same thing is happening in the X axis).

Set WindowStyle="None" and ResizeMode="NoResize" on your main window and you'll see the button now has enough room to draw fine. Better yet, set the right and bottom margin values to 0 and you can now set left and top to whatever you want.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, it's 114 + 108.5 + 50 (heigh of the button) => which leaves 27,5 for Window caption bar. And I just check the button clicked arrow up on my keyboard -> this margin prop is autogenerated by VS. Why it's so bad? WindowStyle="None" removes window's caption and it works. But I'd like to have this caption...
In that case use my second solution. If you want to see where all the borders etc are coming from add a button handler, put a breakpoint there, run your app and click the button so that it hits the breakpoint. Enter "this" into the debugger watch window and click on the little magnifying glass to the right. This will bring up the WPF Tree Visualizer which will allow you to walk down the tree seeing all the elements being rendered and the properties for each (including size).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.