【问题标题】:OnPropertyChanged not working as expected with ObjectListViewOnPropertyChanged 与 ObjectListView 不按预期工作
【发布时间】:2015-09-18 09:34:17
【问题描述】:

这是我的模型类,我对这个问题感兴趣的专栏:

public class Cell : INotifyPropertyChanged
{
    public string TestImageAspect
    {
        get { return testImageAspect; }
        set
        {
            testImageAspect = value;
            Console.WriteLine("OnPropertyChanged => testImageAspect");
            this.OnPropertyChanged("OperationResult");
        }
    }
    private string testImageAspect;
}

ImageList 已准备好所需的图像。在ObjectListView 中,我将相应列的 ImageAspectName 设置为属性名称:

然后点击按钮我运行下面的代码来改变

  Cell c = ...;
  c.TestImageAspect = "success"; // the name exist in ImageList

在上面的代码之后,我看到 OnPropertyChanged 已被调用,但是 UI 没有更新,除非我将鼠标悬停在它必须更改的行上,然后我会看到新图标。我不是在寻找肮脏的解决方法,因为我知道的很少,而是想了解 ObjectListView 是否必须更新 UI 本身。如果是,我做错了什么?

【问题讨论】:

  • 你设置UseNotifyPropertyChanged = true了吗?您可以使用简单的字符串属性进行尝试。可能是图像和 OwnerDraw 模式的错误。
  • 啊,就是这样,太棒了 :) 在我看来,它的文档记录不是很好。
  • 很好,如果您不介意,我会发布答案。
  • @Rev1.0,请继续。

标签: c# objectlistview


【解决方案1】:

ObjectListView 属性 UseNotifyPropertyChanged 必须设置为 true

From the official documentation

如果您设置了 UseNotifyPropertyChanged,那么 ObjectListView 将侦听模型类的更改,并在模型类的属性发生更改时自动更新行。显然,您的模型对象必须实现 INotifyPropertyChanged。

【讨论】:

【解决方案2】:

您能否发布绑定的 XAML - 这可能有助于调试。此外,您的属性称为 TestImageAspect 但您将“OperationResult”传递给 OnPropertyChanged,这有点令人困惑。我不确定 OnPropertyChanged 是否也可以工作。更常见的方法是:-

public class Cell : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string TestImageAspect
    {
        get { return testImageAspect; }
        set
        {
            testImageAspect = value;
            if (PropertyChanged != null)
            {
               PropertyChanged(this, new PropertyChangedEventArgs("TestImageAspect"));
            }

        }
    }
    private string testImageAspect;
}

【讨论】:

  • 我不确定你问的是什么 XAML。是窗体。 “OperationResult”仅在控制台输出中,所以我不会在任何地方传递它。你提出的方式也没有更新 UI。
  • 对不起,我以为是 WPF,而不是 WinForms。您将“OperationResult”传递给 OnPropertyChanged:- this.OnPropertyChanged("OperationResult");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-15
  • 2018-09-12
  • 2015-06-26
  • 2013-05-28
  • 1970-01-01
  • 2019-01-03
相关资源
最近更新 更多