I had a quick google and I quickly looked through StackOverflow but couldn't find anyone else who ran into this problem.
I want to create a close button with a little X in it. The close button will do things such as fading in and out when you hover over an item containing it, change color when you hover, and all the usual fun of WPF. The bottom line, it seems like it's not that difficult, but I ran into one of the strangest problems ever before I got to this stage.
Here is my XAML style for the button:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style x:Key="TabCloseButtonStyle" TargetType="{x:Type Button}"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="Content"> <Setter.Value> <Grid> <Line Stroke="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}" X1="0" Y1="0" X2="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path=ActualWidth, Mode=OneWay}" Y2="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path=ActualHeight, Mode=OneWay}"/> <Line Stroke="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}" X1="0" Y1="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path=ActualHeight, Mode=OneWay}" X2="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path=ActualWidth, Mode=OneWay}" Y2="0"/> </Grid> </Setter.Value> </Setter> </Style> </ResourceDictionary>
And I create my button as a test, for example:
<Window x:Class="WpfTestApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="124" Width="569"> <Grid Background="#2b3c59"> <StackPanel Orientation="Horizontal"> <Button Style="{DynamicResource TabCloseButtonStyle}" HorizontalAlignment="Center" VerticalAlignment="Bottom" Padding="2" Margin="0, 10, 0, 0" MinWidth="50" MinHeight="50"></Button> </StackPanel> </Grid> </Window>
And here, where everything goes terribly wrong. When you launch the application, the button will expand indefinitely, one pixel at a time, horizontally and vertically, until it reaches the height of the window.
Now I can understand why this happens: the lines actually go one unit beyond the width and height of the grid, causing the grid to expand by one, then there is relaying, data binding leads to redrawing the lines, declaring infinity.
So, in order to try to deal with this, I decided to put it in a grid, but then, no matter what I do with HorizontalAlignment and VerticalAlignment, I get both a canvas and a grid having zero width and height, which means I don't get your cross, which is very annoying. If I snap to the ActualWidth and ActualHeight properties of the button, I just end up with X having the upper left corner centered on the center of the button.
Does anyone have any ideas what I can do to fix this? I would be extremely grateful for any pointers - WPF is still a pretty new beast to me.
Thanks!