I’m trying to create a custom Image control, because I have to manipulate it’s sources depending on some events, also i will have pretty large array of such controls. To do that i decided my class (“nfImage”) to be inherited from Image, and i want there a DP (which actually would reflect the events) to I could bind it to a view-model. I’m doing:
class nfImage : Image
{
public static readonly DependencyProperty TagValueProperty =
DependencyProperty.Register("TagValue", typeof(int), typeof(nfImage), new UIPropertyMetadata(0));
public int TagValue
{
get { return (int)GetValue(TagValueProperty); }
set
{
SetValue(TagValueProperty, value);
if (this.Source != null)
{
string uri = (this.Source.ToString()).Substring(0, (this.Source.ToString()).Length - 5) + value.ToString() + ".gif";
ImageBehavior.SetAnimatedSource(this, new BitmapImage(new Uri(uri, UriKind.Absolute)));
}
}
}
}
the problem is it doesn’t work. If I’m setting the value of TagValue from code behind, source changes, but if I’m setting it from xaml (through the dp) nothing happens, bindings don’t work also. How do I achive this?
You can’t use the setter, since XAML doesn’t call it directly: It just calls SetValue(DependencyProperty, value) without going through your setter. You need to handle a PropertyChanged Event: