title: How to draw shape or lines within a WPF button that size to the control without cause the button to expand forever?
body: <p>I had a quick Google, and a quick look around StackOverflow, but couldn't find anyone else who'd run into this problem.</p>
  
  <p>I want to create a close button with a little X in it. The close button will do things like fade in and out when you're hovering over the item containing it, change colour when you mouse over, and all the usual WPF loveliness. Bottom line, it doesn't seem like it should be that difficult, but I'm running into one of the weirdest issues ever before I've even got to this stage.</p>
  
  <p>Here's my XAML style for the button:</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
      &lt;Style x:Key="TabCloseButtonStyle" TargetType="{x:Type Button}"&gt;
          &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/&gt;
          &lt;Setter Property="Background" Value="Transparent"/&gt;
          &lt;Setter Property="Content"&gt;
              &lt;Setter.Value&gt;
                  &lt;Grid&gt;
                      &lt;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}"/&gt;
                      &lt;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"/&gt;
                  &lt;/Grid&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>And I create my button, just as a test, like this:</p>
  
  <pre><code>&lt;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"&gt;
      &lt;Grid Background="#2b3c59"&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
  
              &lt;!-- Other controls removed for clarity --&gt;
  
              &lt;Button Style="{DynamicResource TabCloseButtonStyle}"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Bottom"
                      Padding="2"
                      Margin="0, 10, 0, 0"
                      MinWidth="50"
                      MinHeight="50"&gt;&lt;/Button&gt;
          &lt;/StackPanel&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And here's where it all goes horribly wrong. When you run the application the button will expand infinitely, one pixel at a time, horizontally, and vertically until it hits the height of the window.</p>
  
  <p>Now I can understand why this is happening: the Lines actually go one unit beyond the width and height of the Grid causing the Grid to expand by one unit, then there's a relayout, the data binding causes the lines to redraw, ad infinitum.</p>
  
  <p>So, to try and deal with this I decided to put a  in the grid but then, no matter what I do with HorizontalAlignment and VerticalAlignment, I end up with both the Canvas and the Grid having zero width and height, which means I don't get my cross, which is massively irritating. If I bind to the ActualWidth and ActualHeight properties of the button I just end up with an X that has the top-left corner centred on the centre of the button.</p>
  
  <p>Does anybody have any idea at all what I can do to fix this? I'd be extremely grateful for any pointers - WPF is still a rather new beast to me.</p>
  
  <p>Thanks!</p>