Image.Source not binding properly to Dependency Property in Custom Control


  • I'm trying to bind the Source property of an image in my default template to a Dependency Property. It works properly when I create an instance of the control in XAML but doesn't work when I do it in C#.


    <Style TargetType="local:CheckboxTreeViewItem">
             <Setter Property="BorderThickness" Value="1"/>
             <Setter Property="HorizontalContentAlignment" Value="Left"/>
             <Setter Property="Template"> ....
     <Image x:Name="icon" Width="16" MaxHeight="16" Margin="0,0,4,0" Source="{TemplateBinding IconSource}" /> ....

    C# DP:

    public static readonly DependencyProperty IconSourceProperty =
            DependencyProperty.Register("IconSource", typeof(ImageSource), typeof(CheckboxTreeViewItem), new PropertyMetadata(null, PropertyChanged));
          /// <summary>
          /// gets or sets the Icon displayed on the tree item
          /// </summary>
          public ImageSource IconSource
             get { return (ImageSource)GetValue(IconSourceProperty); }
             set { SetValue(IconSourceProperty, value); }

    C# Instantiation:

    CheckboxTreeViewItem item = new CheckboxTreeViewItem();
             item.Header = rol.Name;
             item.Tag = rol;
             item.IconVisibility = Visibility.Visible;
             item.IconSource = new BitmapImage(new Uri("/Images/local.png", UriKind.Relative));

    Like I said above, this code above just leaves a blank space where the image should be, but if I create this item with XAML it shows the image just fine. When I step through it in debug, I see the property being set properly, but I noticed that if I look at the Image in the OnApplyTemplate() method, the value of IconSource is null even through I just watched it get set. The other properties like header and even visibility persist to the UI thread when the template is actually applied. I'm wondering why IconSource doesn't and how I can get it to do so.


    Thursday, February 09, 2012 12:53 PM


All replies

  • Hi, please post your 

    "PropertyChanged"  event code :)

    Friday, February 10, 2012 1:22 AM
  • I've put in the event handler but haven't done anything with it at the moment. I thought I might be able to reference the Image object through it, but the OnApplyTemplate() still hasn't fired at that point so the object is null.

    static void PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    Friday, February 10, 2012 12:15 PM
  • I tried another way that should have worked but again, it doesn't seem to binding properly.

    I created a copy of my default style and named it CharacteristicTreeItemStyle1. Here I set the style of the item that I dynamically

    created in C# to the new style I created in the xaml page. I have verified that this style has been aplied to the tree view item.

    TreeViewItem.Style = this.TryFindResource("CharacteristicTreeItemStyle1") as Style;

    In the CharacteristicTreeItemStyle1 I set the icon's source to this binding.

    <Image x:Name="icon" Width="16" MaxHeight="16" Margin="0,0,4,0" Source="{Binding MachineID, Converter={StaticResource NullToCheckbuttonConverter}}" Visibility="{TemplateBinding IconVisibility}" />

    This is the converter that I use to create the string for the Source property.

     public class NullToCheckbuttonConverter : IValueConverter
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
             if (value == null)
                return "/Images/KnobRed.png";
                return "/Images/accept.png";
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
             throw new NotImplementedException();

    I have used a very similar trick to this in WPF several times and would expect it to still work. Still can't get these icons to show up unless they are defined in xaml.

    Friday, February 17, 2012 5:12 PM
  • Hi, Download this demo, and replace my textblock control with your image control.


    reply if it's not working.


    Viral :)

    Saturday, February 18, 2012 12:18 AM
  • I don't have anything to unzip a rar, and don't really wan't to buy software that I would rarely use. If you could link to a zip instead, that would be great. Or maybe you know of free rar extracting software.

    Tuesday, February 21, 2012 3:54 PM
  • Hey, you can use 7zip , it will work with rar, zip both :)


    Wednesday, February 22, 2012 4:13 AM