一部のイベントに応じてソースを操作する必要があるため、カスタム Image コントロールを作成しようとしています。また、そのようなコントロールのかなり大きな配列も必要です。そのために、クラス (「nfImage」) を Image から継承することを決定し、ビューモデルにバインドできる DP (実際にはイベントを反映する) が必要です。私がやっている:
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)));
}
}
}
}
問題は、それが機能しないことです。コード ビハインドから TagValue の値を設定している場合、ソースは変更されますが、(dp を介して) xaml から設定している場合は何も起こらず、バインディングも機能しません。どうすればこれを達成できますか?