wpf 属性更改后列表框不刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1123940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:43:24  来源:igfitidea点击:

ListBox doesn't refresh after property changed

wpfdata-bindinglistbox

提问by Jan Remunda

I'm trying to bind two ListBoxes:

我正在尝试绑定两个ListBoxes:

<ListBox SelectionChanged="lbApplications_SelectionChanged"
         ItemsSource="{Binding Path=Applications, 
                       UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />
<ListBox DisplayMemberPath="Message" 
         ItemsSource="{Binding Path=Events, 
                       UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />

Applicationsand Eventsare public properties in Windowclass.

Applications并且EventsWindow类中的公共属性。

I set DataContextto thisto both list boxes and implement INotifyPropertyChangedin Windowclass:

我设置DataContextthis两个列表框并INotifyPropertyChangedWindow课堂上实施:

 private void NotifyPropertyChanged(string info)
 {
   if (PropertyChanged != null)
     PropertyChanged(this, new PropertyChangedEventArgs(info));
 }

And then after adding new item to Applicationsor EventsI call:

然后在向ApplicationsEvents我添加新项目后调用:

 NotifyPropertyChanged("Events");
 NotifyPropertyChanged("Applications");

The issue is that ListBoxis loaded only one time. What am I doing wrong?

问题是ListBox只加载一次。我究竟做错了什么?

回答by exclsr

Let's just look at one of the ListBoxes, since they're both the same, basically.

让我们看看其中一个 ListBoxes,因为它们基本上是相同的。

The code we're concerned about is this:

我们关心的代码是这样的:

<ListBox ItemsSource="{Binding Path=Applications, 
                           UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" />

Since you're new to WPF, let me say you probably don't need UpdateSourceTriggeror Modein there, which leaves us with this:

由于您是 WPF 的新手,让我说您可能不需要UpdateSourceTriggerMode在那里,这给我们留下了以下内容:

<ListBox ItemsSource="{Binding Path=Applications}" />

You mentioned that Applications is a public property in your code-behind. You need it to be a DependencyProperty, and you need it to fire events when it changes -- most people use an ObservableCollectionfor this.

您提到应用程序是您的代码隐藏中的公共属性。你需要它是一个DependencyProperty,并且你需要它在它改变时触发事件——大多数人为此使用ObservableCollection

So your code-behind will have something likethis:

所以你的代码隐藏将是这样的

public ObservableCollection<string> Applications
{
    get { return (ObservableCollection<string>)GetValue(ApplicationsProperty); }
    set { SetValue(ApplicationsProperty, value); }
}

public static readonly DependencyProperty ApplicationsProperty =
    DependencyProperty.Register("Applications", 
    typeof(ObservableCollection<string>), typeof(Window1), 
    new UIPropertyMetadata(null));

Then, where you want to add it, you'll do something like this:

然后,在您想要添加它的地方,您将执行以下操作:

this.Applications = new ObservableCollection<string>();
Applications.Add("Whatever");

Finally, for the "simple" binding syntax to work in the XAML, I usually change the DataContextin my Window (or the root Control element for the file, whatever I'm working in) to

最后,为了在 XAML 中使用“简单”绑定语法,我通常将DataContextWindow(或文件的根 Control 元素,无论我在做什么)更改为

<Window DataContext="{Binding RelativeSource={RelativeSource Self}}" ... >
   ...

Your Applications box will update automatically.

您的应用程序框将自动更新。

回答by Arcturus

The problem is that your property value hasn't changed. It's still the same list, same reference.

问题是你的财产价值没有改变。它仍然是相同的列表,相同的参考。

One solution might be that your collections are of type ObservableCollection. These lists provide events for WPF when you add or remove items.

一种解决方案可能是您的集合类型为ObservableCollection。这些列表在您添加或删除项目时为 WPF 提供事件。