WPF listview数据绑定 - 如何在不标记的情况下强制更新到底层对象?

时间:2021-02-09 07:27:23

I have a WPF ListView that is bound to a BindingList<T>. The binding works like a charm, but I have to tab out of the cell to get the bound property to update....this is an issue because most users don't tab out of the last column before clicking the save button.

我有一个绑定到BindingList 的WPF ListView。绑定就像一个魅力,但我必须从单元格中跳出来获取绑定属性才能更新....这是一个问题,因为大多数用户在单击保存按钮之前不会跳出最后一列。

How do I force the list view to "persist" the changes to the bound DataContext without doing something hackish.

如何强制列表视图“保持”对绑定的DataContext的更改,而不做一些hackish。

1 个解决方案

#1


11  

Bindings in WPF have a property called "UpdateSourceTrigger" which tells the Binding when to update the thing the UI is bound to. By default, it's set to "LostFocus" for the Text property which is what you're most likely using.

WPF中的绑定有一个名为“UpdateSourceTrigger”的属性,它告诉Binding何时更新UI绑定的东西。默认情况下,它为Text属性设置为“LostFocus”,这是您最有可能使用的属性。

Change the trigger to "PropertyChanged" in your binding like this:

在您的绑定中将触发器更改为“PropertyChanged”,如下所示:

Text="{Binding Foo,UpdateSourceTrigger=PropertyChanged}"

... and now the source "Foo" property will be updated as the Text changes in the UI.

...现在,当文本在UI中更改时,源“Foo”属性将更新。

There's also an "Explicit" setting for UpdateSourceTrigger which is handy if you need to hold off writing any changes to the source until, say, the user clicks the OK button.

还有一个UpdateSourceTrigger的“显式”设置,如果您需要暂停写入对源的任何更改,直到用户单击“确定”按钮,这样很方便。

#1


11  

Bindings in WPF have a property called "UpdateSourceTrigger" which tells the Binding when to update the thing the UI is bound to. By default, it's set to "LostFocus" for the Text property which is what you're most likely using.

WPF中的绑定有一个名为“UpdateSourceTrigger”的属性,它告诉Binding何时更新UI绑定的东西。默认情况下,它为Text属性设置为“LostFocus”,这是您最有可能使用的属性。

Change the trigger to "PropertyChanged" in your binding like this:

在您的绑定中将触发器更改为“PropertyChanged”,如下所示:

Text="{Binding Foo,UpdateSourceTrigger=PropertyChanged}"

... and now the source "Foo" property will be updated as the Text changes in the UI.

...现在,当文本在UI中更改时,源“Foo”属性将更新。

There's also an "Explicit" setting for UpdateSourceTrigger which is handy if you need to hold off writing any changes to the source until, say, the user clicks the OK button.

还有一个UpdateSourceTrigger的“显式”设置,如果您需要暂停写入对源的任何更改,直到用户单击“确定”按钮,这样很方便。