绑定依赖属性作为目标不起作用

时间:2014-03-30 21:22:55

标签: data-binding dependency-properties

我创建了一个依赖属性:

public class MapShowViewModel : DependencyObject
{
    public static readonly DependencyProperty MapScaleProperty =
        DependencyProperty.Register("MapScale",
        typeof(double), typeof(MapShowViewModel),
        new FrameworkPropertyMetadata(Convert.ToDouble(1), OnMapScaleChanged));

    private static void OnMapScaleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var mapShowViewModel = d as MapShowViewModel;
        if (mapShowViewModel == null) return;

        mapShowViewModel.MapScale = Convert.ToDouble(e.NewValue);
    }

    public double MapScale
    {
        get { return (double)GetValue(MapScaleProperty); }
        set { SetValue(MapScaleProperty, value); }
    }
}

我在将资源中的ViewModel定义为名称为“MyMap”的元素时将XML绑定在XAML中,并且具有名为“Scale”的双重类型属性,如下所示:

<baseVm:MapShowViewModel x:Key="MapShowViewModel" 
 MapScale="{Binding ElementName=MyMap, Path=Scale,
 UpdateSourceTrigger=PropertyChanged,     
 Mode=TwoWay, Converter={StaticResource MapScaleConverter}}" />

但是当Map元素的Scale属性发生变化时,MapScale属性的值不会改变。

0 个答案:

没有答案