0

考虑这个附加属性:

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);
    }
}

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}"

因此,我将视图的TestImage属性绑定到视图模型的TestImage属性!然后,从代码隐藏我更新属性:

this.TestImage = image;

这不会触发我的视图模型中的设置器。

这是什么原因造成的?

4

1 回答 1

0

Binding开启模式将TwoWay解决问题。

于 2013-01-28T16:30:22.507 回答