更新datacontext时,自定义控件依赖项属性未更改

时间:2018-01-19 20:28:12

标签: c# wpf mvvm data-binding

我有一个示例程序来重新创建我遇到的问题。

控制:

@HostListener('window:keyup', ['$event'])
  keyEvent(event: KeyboardEvent) {
    if (event.keyCode === 27) {        
        if (this.isOpen && this.$modal[0].style.zIndex === (Modal.zIndex).toString()) {
            this.escape.emit();
        }  
    }
}

主窗口

public class PropertyUpdateControl : Control
{
    public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
        nameof(Value), typeof(double), typeof(PropertyUpdateControl),
        new FrameworkPropertyMetadata(default(double), FrameworkPropertyMetadataOptions.None, ValueProperty_Changed));

    public double Value
    {
        get { return (double)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    private static void ValueProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var lPropertyUpdateControl = (PropertyUpdateControl) d;
        lPropertyUpdateControl.Value_Changed((double)e.OldValue, (double)e.NewValue);
    }

    private void Value_Changed(double oldValue, double newValue)
    {
        this.Value = newValue;
        this.Label = newValue.ToString();
    }


    public static readonly DependencyProperty LabelProperty = DependencyProperty.Register(
        nameof(Label), typeof(string), typeof(PropertyUpdateControl), new PropertyMetadata("Label"));

    public string Label
    {
        get { return (string)GetValue(LabelProperty); }
        set { SetValue(LabelProperty, value); }
    }

}

XAML

public class MainWindowViewModel : ViewModelBase
{
    private PropertyUpdateViewModel mPropertyUpdateViewModel;

    public MainWindowViewModel()
    {
        this.PropertyUpdateViewModel = new PropertyUpdateViewModel();
        this.ChangeCommand = new RelayCommand(this.ChangeImplementation);
    }

    public ICommand ChangeCommand { get; set; }

    public void ChangeImplementation()
    {
        if (this.PropertyUpdateViewModel == null)
        {
            this.PropertyUpdateViewModel = new PropertyUpdateViewModel();
        }
        else
        {
            this.PropertyUpdateViewModel = null;
        }
    }

    public PropertyUpdateViewModel PropertyUpdateViewModel
    {
        get { return this.mPropertyUpdateViewModel;}
        set { this.Set(ref this.mPropertyUpdateViewModel, value); }
    }
}

public class PropertyUpdateViewModel : ViewModelBase
{
    private double mValue;

    private static int sInstanceCounter = 0;

    public PropertyUpdateViewModel()
    {
        Interlocked.Increment(ref sInstanceCounter);
        this.Value = sInstanceCounter;
    }

    public double Value
    {
        get { return this.mValue; }
        set { this.Set(ref this.mValue, value); }
    }
}

所以基本上控制似乎只更新一次。即使将视图模型设置为null,然后设置为具有递增值的PropertyUpdateViewModel的新实例,标签也将始终读为1。为什么PropertyUpdateControl Value_Changed只调用一次,即使在datacontext更改后也从未再次调用过?

1 个答案:

答案 0 :(得分:0)

我发现问题在

public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
    nameof(Value), typeof(double), typeof(PropertyUpdateControl),
    new FrameworkPropertyMetadata(default(double), FrameworkPropertyMetadataOptions.None, ValueProperty_Changed));

需要FrameworkPropertyMetadataOptions.BindsTwoWayByDefault

此帖子提供了有关任何感兴趣的人https://www.tutorialspoint.com/wpf/wpf_data_binding.htm

的更多信息