当源更改时,Combobox SelectedItem不会更新 [英] Combobox SelectedItem doesn't update when source changes

查看:287
本文介绍了当源更改时,Combobox SelectedItem不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现INotifyPropertyChanged的视图模型.在此viewModel上是一个名为SubGroupingView的属性.此属性绑定到组合框的选定项目.当我更改组合框时,可以很好地更新source属性,但是当我更改source属性或初始化控件时,combobox.selectedItem不能反映该属性中存在的内容.
以下是一些入门代码:

I have a viewmodel which implement INotifyPropertyChanged. On this viewModel is a property called SubGroupingView. This property is bound to the selected item of a combo box. When i change the combo box, the source property is being updated fine, but when I change the source property or when the control is initialized, the combobox.selectedItem is NOT reflecting what exists in the property.
Here is some code to get you started:

<ComboBox Grid.Column="3" Grid.Row="1" 
          Margin="0,1,4,1" 
          SelectedItem="{Binding Path=SubGroupingView, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, diag:PresentationTraceSources.TraceLevel=High}" 
          ItemsSource="{Binding Columns}" 
          DisplayMemberPath="DisplayName">

该属性引发PropertyChanged事件,TraceSource输出显示绑定已检测到该绑定并传输了该值,只是组合框未反映该值.任何想法都将受到欢迎!

The property raises the PropertyChanged event and the TraceSource output shows me that the binding detected it and transferred the value, its just that the combobox isn't reflecting it. Any ideas would be most welcome!


跟踪源的输出是这样的:


output from the trace source is this:

System.Windows.Data Warning: 91 : BindingExpression (hash=23631369): Got PropertyChanged event from ReportViewModel (hash=52844413)  
System.Windows.Data Warning: 97 : BindingExpression (hash=23631369): GetValue at level 0 from ReportViewModel (hash=52844413) using RuntimePropertyInfo(SubGroupingView):         DataColumnViewModel (hash=58231222)  
System.Windows.Data Warning: 76 : BindingExpression (hash=23631369): TransferValue - got raw value DataColumnViewModel (hash=58231222)  
System.Windows.Data Warning: 80 : BindingExpression (hash=23631369): TransferValue - implicit converter produced DataColumnViewModel (hash=58231222)  
System.Windows.Data Warning: 85 : BindingExpression (hash=23631369): TransferValue - using final value DataColumnViewModel (hash=58231222)  

以下是源属性的代码:

public class ReportViewModel : ViewModelBase, IReportTemplate
{
    public DataColumnViewModel SubGroupingView
    {
        get
        {
            return GetViewModel(_report.SubGrouping);
        }
        set
        {
            if (_report.SubGrouping == value.ColumnName)
                return;
            _report.SubGrouping = value.ColumnName;
            RefreshDataSeries();
            base.OnPropertyChanged("SubGroupingView");
            base.OnPropertyChanged("IsReady");
        }

    }
}

注意:ViewModelBase实现INotifyPropertyChange.

答案
我重载了==,!=运算符,GetHashCode()Equals(object),现在它运行良好.感谢您的所有帮助!

ANSWER
I overloaded the ==, != operators, GetHashCode(), and Equals(object) and now it is working nicely. Thanks for all of your help!

推荐答案

从SubGroupingView返回的对象必须与ComboBox.Items中的对象之一相等"(这意味着它必须在Columns集合中).因此,如果执行"a.Equals(b)",则需要返回true.

The object returned from your SubGroupingView must be "equal" to one of the objects in the ComboBox.Items (which means it must be in your Columns collection). So if you perform an "a.Equals(b)", it would need to return true.

如果它们在功能上相同,但在比较时未返回true,则这就是您的问题.您将需要返回相同的对象,或者重写Equals方法(并可能重写==和!=运算符).

If they are functionally the same, but not returning true when compared then that's your problem. You would need to either return the same object, or override the Equals method (and potentially the == and != operators).

如果这是您的问题,则与此问题.

If this is your issue, it's the same problem as in this question.

这篇关于当源更改时,Combobox SelectedItem不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆