Consider this attached property:
public static readonly DependencyProperty TestImageProperty =
DependencyProperty.RegisterAttached("TestImage",
typeof(BitmapSource), typeof(TestView), new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.AffectsRender));
public static void SetTestImage(UIElement element, BitmapSource value)
{
element.SetValue(TestImageProperty, value);
}
public static BitmapSource GetTestImage(UIElement element)
{
return (BitmapSource)element.GetValue(TestImageProperty);
}
public BitmapSource TestImage
{
get
{
return GetTestImage(this);
}
set
{
SetTestImage(this, value);
}
}
And the XAML:
<common:UserControlBase x:Class="MyViews.TestView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:MyViews"
xmlns:common="clr-namespace:MyCommon.Common;assembly=MyCommon.Common"
xmlns:mefed="clr-namespace:MEFedMVVM.ViewModelLocator;assembly=MEFedMVVM.WPF"
mefed:ViewModelLocator.SharedViewModel="MyViewModel"
views:TestView.TestImage="{Binding TestImage}"
Hence, I’m binding the view’s TestImage property to the viewmodel’s TestImage property! Then, from code-behind I update the property:
this.TestImage = image;
This does NOT trigger the setter in my viewmodel.
What is causing this?
Put the
BindingonTwoWayMode will resolve the problem.