gpt4 book ai didi

wpf - 尽管属性值正在更新,但在数据绑定(bind)属性的设置 block 中没有命中断点(和额外代码)

转载 作者:行者123 更新时间:2023-12-03 10:44:57 24 4
gpt4 key购买 nike

我正在使用从列表框派生的自定义控件,但具有附加功能。关键特性之一是在控件上添加了可绑定(bind)的 SelectedItems 属性,因此 View 模型可以跟踪控件中所做的多个选择。绑定(bind)确实有效 - 当您在控件中选择项目时, View 模型的属性会更新。但是,我想将 INotifyDataErrorInfo 验证添加到 View 模型中,因此我实现了接口(interface)并在 View 模型中数据绑定(bind)属性的 set block 中添加了对我的验证方法的调用。由于某种原因,即使我正在更新 View 中的控件,并且正在验证 View 模型的属性值实际上是否被正确更改以匹配控件,也不会调用 set block 。

我知道,当我使用标准 WPF 控件(例如 TextBox)进行绑定(bind)时,源( View 模型)属性的 set block 会在目标( View )属性更改时被调用。有没有理由不在这里调用它?

找到我使用的自定义控件here .这是我在 View 模型上的属性(我有控制台输出只是为了确保代码没有被调用):

    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 并且设置 block 仍然没有被命中):
                <src:MultiComboBox SelectionMode="Multiple" 
VerticalAlignment="Center"
ItemsSource="{Binding testList}"
SelectedItems="{Binding testListSelections, ValidatesOnNotifyDataErrors=True}"/>

这是自定义控件上的 SelectedItems 属性(作者重写了基本只读 SelectedItems 属性以允许绑定(bind)):
    /// <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 列表应该只设置一次,然后对同一个列表进行更改。
然后您可以检查操作是添加、删除还是重置,并相应地执行验证。

关于wpf - 尽管属性值正在更新,但在数据绑定(bind)属性的设置 block 中没有命中断点(和额外代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30418466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com