title: WPF Custom TabItem with Close Button
body: <p>I'm using WPF and C#. I am trying to create a custom TabItem which contains a close button. I did follow a tutorial and managed to get it working - however I wanted to use Images for my close buttons. Upon trying this, I came across some exceptions.</p>
  
  <p>A first chance exception of type 'System.Deployment.Application.InvalidDeploymentException' occurred in System.Deployment.dll
  A first chance exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll
  A first chance exception of type 'System.IO.DirectoryNotFoundException' occurred in PresentationCore.dll
  A first chance exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll</p>
  
  <p>I believe this is complaining about the Image Source not being found. To set the Image Source I used the properties pane and selecting the appropriate resource in the Source drop down menu. This should be fine as it is, but it is not fine at all.</p>
  
  <p>I was wondering if anyone could give me some heads up on what to do? It's weird, because if it says the resources isn't found, well the resources are there. I added the images using Resources.resx and it added them into my Resources folder. It's all there... just my XAML does not like it.</p>
  
  <p>Here is my XAML</p>
  
  <pre><code>&lt;UserControl x:Class="WpfApplication1.CloseableHeader"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    d:DesignHeight="23" d:DesignWidth="81" Margin="0"&gt;
  &lt;UserControl.Resources&gt;
      &lt;Style TargetType="Button" x:Key="close_hover"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                      &lt;Image Source="pack://siteoforigin:,,,/Resources/CloseIco.png" /&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
      &lt;Style TargetType="Button" x:Key="close_normal"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                      &lt;Image Source="pack://siteoforigin:,,,/Resources/CloseIco_BW.png" /&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/UserControl.Resources&gt;
  &lt;Grid&gt;
      &lt;Button
          Name="button_close"
          ToolTip="Close"
          HorizontalAlignment="Right"
          VerticalAlignment="Center"
          Margin="0,0,5,0"
          Width="14"
          Height="14"
          Style="{DynamicResource close_normal}"/&gt;
      &lt;Label Content="TabItem"  Height="23" HorizontalAlignment="Left" 
        Margin="4,1,0,0" Name="label_TabTitle" VerticalAlignment="Top" 
        FontFamily="Courier" FontSize="12" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>I should also note that in the Designer View on Visual Studio, it displays the tabs as they should be (with the images!).</p>
  
  <p>Thanks in advance.</p>
  
  <hr>
  
  <p>UPDATE</p>
  
  <hr>
  
  <p>I have now gotten further in my project, the only problem I face is that the paths on Visual Studio do not exist, but when I debug - they do exist. The resources folder is inside bin/Debug so that the images will display on debug... but this means I'll always have errors in the Designer as a result. Is there a way to fix this so it'll work for both?</p>
  
  <p>Here is the updated XAML (quite a change)</p>
  
  <pre><code>&lt;UserControl x:Class="WpfApplication1.CloseableHeader"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    d:DesignHeight="23" Margin="0"&gt;
  &lt;UserControl.Resources&gt;
      &lt;Style TargetType="{x:Type Button}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                      &lt;Border Name="border"
                              BorderThickness="0"
                      &gt;
                          &lt;Border.Background&gt;
                              &lt;ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/CloseIco_BW.png" /&gt;
                          &lt;/Border.Background&gt;
                      &lt;/Border&gt;
                      &lt;ControlTemplate.Triggers&gt;
                          &lt;Trigger Property="IsMouseOver" Value="True"&gt;
                              &lt;Setter Property="Background" TargetName="border"&gt;
                                  &lt;Setter.Value&gt;
                                      &lt;ImageBrush ImageSource="pack://siteoforigin:,,,/Resources/CloseIco.png" /&gt;
                                  &lt;/Setter.Value&gt;
                              &lt;/Setter&gt;
                          &lt;/Trigger&gt;
                      &lt;/ControlTemplate.Triggers&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/UserControl.Resources&gt;
  &lt;Grid Background="White"&gt;
      &lt;Button
          Name="button_close"
          ToolTip="Close"
          HorizontalAlignment="Right"
          VerticalAlignment="Center"
          Margin="0,0,5,0"
          Width="14"
          Height="14"/&gt;
      &lt;Label Content="TabItem"  Height="23" HorizontalAlignment="Left" 
        Margin="4,1,19,0" Name="label_TabTitle" VerticalAlignment="Top" 
        FontFamily="Courier" FontSize="12" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>