title: How to Pass Image to a Button in UserControl using Dependency Property
body: <p>I have created an UserControl. It has a Button and a textblock. I want to pass an Image to that Button using Dependency property. But when I try to call that usercontrol in some other page, its showing some error. This is my code..</p>
  
  <p>User Control.xaml:</p>
  
  <pre><code>&lt;UserControl x:Class="....UserControl"
           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="300" d:DesignWidth="300" &gt;
  &lt;Grid&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="Auto" /&gt;
          &lt;RowDefinition Height="Auto" /&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;Border Grid.Row="0"
          Name="borMain"
          Style="{StaticResource ButtonImageTextBorderStyle}"
          MouseEnter="borMain_MouseEnter"
          MouseLeave="borMain_MouseLeave"
          PreviewMouseLeftButtonDown="borMain_MouseLeftButtonDown" &gt;
          &lt;VisualStateManager.VisualStateGroups&gt;
              &lt;VisualStateGroup Name="MouseStates"&gt;
                  &lt;VisualState Name="MouseEnter"&gt;
                      &lt;Storyboard&gt;
                          &lt;ColorAnimation To="Black"
                            Duration="0:0:00.1"
                            Storyboard.TargetName="borMain"
                            Storyboard.TargetProperty="BorderBrush.Color" /&gt;
                          &lt;ColorAnimation To="Black"
                            Duration="0:0:00.1"
                            Storyboard.TargetName="borMain"
                            Storyboard.TargetProperty="Background.Color" /&gt;
                          &lt;ThicknessAnimation To="4,1,4,4"
                                Duration="0:0:00.1"
                                Storyboard.TargetName="borMain"
                                Storyboard.TargetProperty="Margin" /&gt;
                      &lt;/Storyboard&gt;
                  &lt;/VisualState&gt;
                  &lt;VisualState Name="MouseLeave" /&gt;
                  &lt;VisualStateGroup.Transitions&gt;
                      &lt;VisualTransition To="MouseLeave" /&gt;
                      &lt;VisualTransition To="MouseEnter" /&gt;
                  &lt;/VisualStateGroup.Transitions&gt;
              &lt;/VisualStateGroup&gt;
          &lt;/VisualStateManager.VisualStateGroups&gt;
          &lt;Button Content="{Binding Path=AppBarContent}"                  
           Style="{StaticResource ButtonImageTextImageStyle}" /&gt;
      &lt;/Border&gt;
  
      &lt;TextBlock Grid.Row="1"
             Name="tbText"
             Style="{StaticResource ButtonImageTextTextBlockStyle}"
             Text="{Binding Path=Text}" /&gt;
     &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>UserControl.xaml.cs:</p>
  
  <pre><code> [DefaultEvent("Click")]
     public partial class SystemUnitUserControl : UserControl
     {
        public UserControl()
        {
          InitializeComponent();
          this.DataContext = this;
        }
      #region Text Property
      public string Text
      {
          get { return (string)GetValue(TextProperty); }
          set
          {
              SetValue(TextProperty, value);
              if (string.IsNullOrEmpty(value))
                  tbText.Visibility = Visibility.Collapsed;
          }
      }
  
  
      #region AppBarContent Property
      public Image AppBarContent
      {
          get { return (Image)GetValue(AppBarContentProperty); }
          set { SetValue(AppBarContentProperty, value); }
      }
  
        public static readonly DependencyProperty AppBarContentProperty =
          DependencyProperty.Register("AppBarContent", typeof(Image),  typeof(SystemUnitUserControl), null);
      #endregion
  
      #region MouseLeftDown Event
      private void borMain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
      {
          RaiseClick(e);
      }
      #endregion
  
      #region Click Event Procedure
      public delegate void ClickEventHandler(object sender, RoutedEventArgs e);
      public event ClickEventHandler Click;
  
      protected void RaiseClick(RoutedEventArgs e)
      {
          if (null != Click)
              Click(this, e);
      }
      #endregion
  
      #region Visual State Animations
      private void borMain_MouseEnter(object sender, MouseEventArgs e)
      {
          VisualStateManager.GoToElementState(borMain, "MouseEnter", true);
      }
  
      private void borMain_MouseLeave(object sender, MouseEventArgs e)
      {
          VisualStateManager.GoToElementState(borMain, "MouseLeave", true);
      }
      #endregion
  </code></pre>
  
  <p>I am calling this Usercontrol in other page. </p>
  
  <p>Window1.xaml:</p>
  
  <pre><code>...
   xmlns:my="clr-namespace:....UserControls"
  ...
  
           &lt;my:UserControl x:Name="actionRectDuct"   
                           AppBarContent="F:\..\..\Assets\offline.jpg"
                           Text="Button 1" /&gt;                         
  </code></pre>
  
  <p>Its not going into that page. Its showing the following error..</p>
  
  <p>A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll</p>
  
  <p>Additional information: 'Set property 'UnitUserControl.AppBarContent' threw an exception.' Line number '38' and line position '41'.    </p>
  
  <p>I want to pass image on the AppBarContent.. how can i do this??  </p>