Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 虽然正在更新属性值,但在数据绑定属性的集合块中未命中断点(和额外代码)_Wpf_Xaml_Mvvm_Data Binding - Fatal编程技术网

Wpf 虽然正在更新属性值,但在数据绑定属性的集合块中未命中断点(和额外代码)

Wpf 虽然正在更新属性值,但在数据绑定属性的集合块中未命中断点(和额外代码),wpf,xaml,mvvm,data-binding,Wpf,Xaml,Mvvm,Data Binding,我使用的是从列表框派生的自定义控件,但添加了一些特性。关键特性之一是在控件上添加了可绑定的SelectedItems属性,因此视图模型可以跟踪控件中的多个选择。绑定不起作用-当您在控件中选择项时,视图模型的属性将更新。但是,我想将INotifyDataErrorInfo验证添加到视图模型中,因此我实现了该接口,并在viewmodel的数据绑定属性的set块中添加了对我的验证方法的调用。由于某些原因,从未调用set块,即使我正在更新视图中的控件,并且正在验证视图模型的属性值是否已正确更改以匹配控件

我使用的是从列表框派生的自定义控件,但添加了一些特性。关键特性之一是在控件上添加了可绑定的SelectedItems属性,因此视图模型可以跟踪控件中的多个选择。绑定不起作用-当您在控件中选择项时,视图模型的属性将更新。但是,我想将INotifyDataErrorInfo验证添加到视图模型中,因此我实现了该接口,并在viewmodel的数据绑定属性的set块中添加了对我的验证方法的调用。由于某些原因,从未调用set块,即使我正在更新视图中的控件,并且正在验证视图模型的属性值是否已正确更改以匹配控件

我知道,当我对标准WPF控件(如文本框)使用绑定时,当目标视图属性更改时,将调用源视图模型属性的set块。有没有理由不在这里叫它

找到了我正在使用的自定义控件。这是我在viewmodel上的属性,我在那里有控制台输出,只是为了确保代码没有被调用:

    private ObservableCollection<Car> _testListSelections;
    public ObservableCollection<Car> testListSelections
    {
        get
        {
            return _testListSelections;
        }
        set
        {
            Console.WriteLine("Value changed.");
            _testListSelections = value;
            OnPropertyChanged("testListSelections");
            Validate();
        }
    }
这是我的XAML注意事项,我在这里不需要使用Mode=TwoWay,因为我使用的是ObservableCollection,但我尝试指定Mode=TwoWay,设置块仍然没有命中:

                <src:MultiComboBox SelectionMode="Multiple" 
                               VerticalAlignment="Center"
                               ItemsSource="{Binding testList}"
                               SelectedItems="{Binding testListSelections, ValidatesOnNotifyDataErrors=True}"/>
这是自定义控件上的SelectedItems属性。为了允许绑定,作者重写了基本只读SelectedItems属性:

    /// <summary>
    /// The SelectedItems dependency property. Access to the values of the items that are 
    /// selected in the selectedItems box. If SelectionMode is Single, this property returns an array
    /// of length one.
    /// </summary>
    public static new readonly DependencyProperty SelectedItemsProperty =
       DependencyProperty.Register("SelectedItems", typeof(IList), typeof(BindableListBox),
       new FrameworkPropertyMetadata(
              (d, e) =>
              {
                  // When the property changes, update the selected values in the selectedItems box.
                  (d as BindableListBox).SetSelectedItemsNew(e.NewValue as IList);
              }));
    /// <summary>
    /// Get or set the selected items.
    /// </summary>
    public new IList SelectedItems
    {
        get
        {
            return GetValue(SelectedItemsProperty) as IList;
        }
        set { SetValue(SelectedItemsProperty, value); }
    }

您应该在列表的OnCollectionChanged事件中执行验证。 SelectedItems列表只应设置一次,然后对同一列表进行更改。 然后,您可以检查操作是添加、删除还是重置,并相应地执行验证