使用 MVVM 取消 WPF 中的组合框选择
我的 WPF 应用程序中有一个组合框:
<ComboBox ItemsSource="{Binding CompetitorBrands}" DisplayMemberPath="Value"
SelectedValuePath="Key" SelectedValue="{Binding Path=CompMfgBrandID, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" Text="{Binding CompMFGText}"/>
绑定到 KeyValuePair
的集合
这是我的 ViewModel 中的 CompMfgBrandID 属性:
public string CompMfgBrandID
{
get { return _compMFG; }
set
{
if (StockToExchange != null && StockToExchange.Where(x => !string.IsNullOrEmpty(x.EnteredPartNumber)).Count() > 0)
{
var dr = MessageBox.Show("Changing the competitor manufacturer will remove all entered parts from the transaction. Proceed?",
"Transaction Type", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dr != DialogResult.Yes)
return;
}
_compMFG = value;
StockToExchange.Clear();
...a bunch of other functions that don't get called when you click 'No'...
OnPropertyChanged("CompMfgBrandID");
}
}
如果您选择“yes”,它的行为将按预期进行。项目被清除并调用剩余的函数。如果我选择“否”,它会返回并且不会清除我的列表或调用任何其他函数,这很好,但组合框仍然显示新的选择。当用户选择“否”时,我需要它恢复到原始选择,就好像什么都没有改变一样。我怎样才能做到这一点?我还尝试在代码隐藏中添加 e.Handled = true
,但无济于事。
I've got a combobox in my WPF application:
<ComboBox ItemsSource="{Binding CompetitorBrands}" DisplayMemberPath="Value"
SelectedValuePath="Key" SelectedValue="{Binding Path=CompMfgBrandID, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" Text="{Binding CompMFGText}"/>
Bound to a collection of KeyValuePair<string, string>
Here is the CompMfgBrandID property in my ViewModel:
public string CompMfgBrandID
{
get { return _compMFG; }
set
{
if (StockToExchange != null && StockToExchange.Where(x => !string.IsNullOrEmpty(x.EnteredPartNumber)).Count() > 0)
{
var dr = MessageBox.Show("Changing the competitor manufacturer will remove all entered parts from the transaction. Proceed?",
"Transaction Type", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dr != DialogResult.Yes)
return;
}
_compMFG = value;
StockToExchange.Clear();
...a bunch of other functions that don't get called when you click 'No'...
OnPropertyChanged("CompMfgBrandID");
}
}
If you choose "yes", it behaves as expected. Items are cleared and the remaining functions are called. If I choose 'No', it returns and doesn't clear my list or call any of the other functions, which is good, but the combobox still displays the new selection. I need it to revert back to the original selection, as if nothing had changed, when the user picks 'No'. How can I accomplish this? I also tried adding e.Handled = true
in codebehind, to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我的做法与上面的 splintor 类似。
您的视图:
下面是视图后面的代码文件中的事件处理程序“ComboBox_SelectionChanged”的代码。例如,如果您的视图是 myview.xaml,则此事件处理程序的代码文件名应为 myview.xaml.cs
I did it in a similar way to what splintor has above.
Your view:
Below is the code for the event handler "ComboBox_SelectionChanged" from the code file behind the view. For example, if you view is myview.xaml, the code file name for this event handler should be myview.xaml.cs
以下是我使用的一般流程(不需要任何行为或 XAML 修改):
(如果您的业务逻辑要求所选项目不处于无效状态,我建议将其移至模型端)。此方法对于使用单选按钮呈现的列表框也很友好,因为使 SelectedItem setter 尽快退出不会阻止单选按钮在弹出消息框时突出显示。
我将所有撤消逻辑放入处理程序中,并使用 SynchronizationContext.Post() 调用该事件
(顺便说一句:SynchronizationContext.Post 也适用于 Windows 应用商店应用程序。因此,如果您共享 ViewModel 代码,此方法仍然有效)。
Here is the general flow that I use (doesn't need any behaviors or XAML modifications):
(If your business logic requires the selected item to not be in an invalid state, I suggest moving that to the Model side). This approach is also friendly to ListBoxes that are rendered using Radio Buttons as making the SelectedItem setter exit as soon as possible will not prevent radio buttons from being highlighted when a message box pops out.
I put any undo logic in a handler and call that using SynchronizationContext.Post()
(BTW: SynchronizationContext.Post also works for Windows Store Apps. So if you have shared ViewModel code, this approach would still work).
我认为问题在于 ComboBox 在设置绑定属性值后将所选项目设置为用户操作的结果。因此,无论您在 ViewModel 中执行什么操作,Combobox 项都会发生变化。我发现了一种不同的方法,您不必改变 MVVM 模式。这是我的示例(抱歉,它是从我的项目复制的,与上面的示例不完全匹配):
不同之处在于我完全清除了项目集合,然后用之前存储的项目填充它。当我使用 ObservableCollection 泛型类时,这会强制 Combobox 进行更新。然后我将所选项目设置回之前设置的所选项目。对于很多项目不建议这样做,因为清除和填充组合框有点昂贵。
I think the problem is that the ComboBox sets the selected item as a result of the user action after setting the bound property value. Thus the Combobox item changes no matter what you do in the ViewModel. I found a different approach where you don't have to bend the MVVM pattern. Here's my example (sorry that it is copied from my project and does not exactly match the examples above):
The difference is that I completely clear the items collection and then fill it with the items stored before. This forces the Combobox to update as I'm using the ObservableCollection generic class. Then I set the selected item back to the selected item that was set previously. This is not recommended for a lot of items because clearing and filling the combobox is kind of expensive.
我想完成 splintor 的回答,因为我偶然发现了
OnSelectedItemChanged
中延迟初始化的问题:在分配 AssociatedObject 之前引发 OnSelectedItemChanged 时,使用 System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke 可以具有不需要的副作用,例如尝试使用组合框选择的默认值初始化 newValue。
因此,即使您的 ViewModel 是最新的,该行为也会触发从 ViewModel 的
SelectedItem
当前值更改为存储在e.NewValue
中的 ComboBox 的默认选择。如果您的代码触发对话框,则会警告用户有更改,尽管没有更改。我无法解释为什么会发生这种情况,可能是时间问题。这是我的修复方法
I would like to complete splintor's answer because I stumbled upon a problem with the delayed initialization in
OnSelectedItemChanged
:When OnSelectedItemChanged is raised before AssociatedObject is assigned, using the
System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke
can have unwanted side effects, such as trying to initialize the newValue with the default value of the combobox selection.So even if your ViewModel is up to date, the behaviour will trigger a change from the ViewModel's
SelectedItem
current value to the default selection of the ComboBox stored ine.NewValue
. If your code triggers a Dialog Box, the user will be warned of a change although there is none. I can't explain why it happens, probably a timing issue.Here's my fix
--Xaml
--ViewModel
--Xaml
--ViewModel
.NET 4.5.1+ 的非常简单的解决方案:
它在所有情况下都适合我。
您可以回滚组合框中的选择,只需触发 NotifyPropertyChanged 而不分配值。
Very simple solution for .NET 4.5.1+:
It's works for me in all cases.
You can rollback selection in combobox, just fire NotifyPropertyChanged without value assignment.
要在 MVVM 下实现此目的....
1] 有一个附加行为来处理 ComboBox 的
SelectionChanged
事件。此事件是通过一些具有Handled
标志的事件参数引发的。但将其设置为 true 对于SelectedValue
绑定来说是没有用的。无论事件是否被处理,绑定都会更新源。2] 因此,我们将
ComboBox.SelectedValue
绑定配置为TwoWay
和Explicit
。3] 只有当您的检查得到满足并且消息框显示
是
时,我们才会执行BindingExpression.UpdateSource()
。否则,我们只需调用BindingExpression.UpdateTarget()
即可恢复到旧的选择。在下面的示例中,我有一个绑定到窗口数据上下文的
KeyValuePair
列表。ComboBox.SelectedValue
绑定到Window
的一个简单的可写MyKey
属性。XAML ...
其中
MyDGSampleWindow
是Window
的 x:Name。代码隐藏...
和附加行为
在行为中我使用
ComboBox.Tag
属性来暂时存储一个标志,当我们恢复到旧的选定值时,该标志会跳过重新检查。如果这有帮助,请告诉我。
To achieve this under MVVM....
1] Have an attached behavior that handles the
SelectionChanged
event of the ComboBox. This event is raised with some event args that haveHandled
flag. But setting it to true is useless forSelectedValue
binding. The binding updates source irrespective of whether the event was handled.2] Hence we configure the
ComboBox.SelectedValue
binding to beTwoWay
andExplicit
.3] Only when your check is satisfied and messagebox says
Yes
is when we performBindingExpression.UpdateSource()
. Otherwise we simply call theBindingExpression.UpdateTarget()
to revert to the old selection.In my example below, I have a list of
KeyValuePair<int, int>
bound to the data context of the window. TheComboBox.SelectedValue
is bound to a simple writeableMyKey
property of theWindow
.XAML ...
Where
MyDGSampleWindow
is the x:Name of theWindow
.Code Behind ...
And the attached behavior
In the behavior I use
ComboBox.Tag
property to temporarily store a flag that skips the rechecking when we revert back to the old selected value.Let me know if this helps.
这可以使用 Blend 的 一般行为。
该行为定义了一个名为
SelectedItem
的依赖属性,您应该将绑定放入此属性中,而不是放在 ComboBox 的SelectedItem
属性中。该行为负责将依赖属性中的更改传递给 ComboBox(或更一般地说,传递给选择器),并且当选择器的SelectedItem
更改时,它会尝试将其分配给自己的SelectedItem
属性。如果分配失败(可能是因为绑定的 VM 属性设置器拒绝分配),该行为将使用其SelectedItem
属性的当前值更新选择器的SelectedItem
。由于各种原因,您可能会遇到选择器中的项目列表被清除并且所选项目变为空的情况(请参阅这个问题)。在这种情况下,您通常不希望 VM 属性变为 null。为此,我添加了 IgnoreNullSelection 依赖属性,默认情况下该属性为 true。这应该可以解决这样的问题。
这是
CancellableSelectionBehavior
类:这是在 XAML 中使用它的方法:
这是 VM 属性的示例:
This can be achieved in a generic and compact way using Blend's Generic Behavior.
The behavior defines a dependency property named
SelectedItem
, and you should put your binding in this property, instead of in the ComboBox'sSelectedItem
property. The behavior is in charge of passing changes in the dependency property to the ComboBox (or more generally, to the Selector), and when the Selector'sSelectedItem
changes, it tries to assign it to the its ownSelectedItem
property. If the assignment fails (probably because the bound VM proeprty setter rejected the assignment), the behavior updates the Selector’sSelectedItem
with the current value of itsSelectedItem
property.For all sorts of reasons, you might encounter cases where the list of items in the Selector is cleared, and the selected item becomes null (see this question). You usually don't want your VM property to become null in this case. For this, I added the IgnoreNullSelection dependency property, which is true by default. This should solve such problem.
This is the
CancellableSelectionBehavior
class:This is the way to use it in XAML:
and this is a sample of the VM property:
我在另一个线程上找到了用户 shaun 对这个问题的更简单的答案:
https://stackoverflow.com/a/6445871/2340705
基本问题是属性更改事件被吞没。有些人会将此称为错误。要解决此问题,请使用 Dispatcher 中的 BeginInvoke 来强制将属性更改事件放回到 UI 事件队列的末尾。这不需要更改 xaml,不需要额外的行为类,并且只需对视图模型更改一行代码。
I found a much simpler answer to this question by user shaun on another thread:
https://stackoverflow.com/a/6445871/2340705
The basic problem is that the property changed event gets swallowed. Some would called this a bug. To get around that use BeginInvoke from the Dispatcher to force the property changed event to be put back onto the end of UI event queue. This requires no change to the xaml, no extra behavior classes, and a single line of code changed to the view model.
问题是,一旦 WPF 使用属性 setter 更新值,它就会忽略该调用中任何进一步的属性更改通知:它假设它们将作为 setter 的正常部分发生,并且不会产生任何后果,即使您确实有将属性更新回原始值。
我解决这个问题的方法是允许字段更新,但也在调度程序上排队操作以“撤消”更改。该操作会将其设置回旧值并触发属性更改通知,以使 WPF 意识到它实际上并不是它认为的新值。
显然,应该设置“撤消”操作,这样它就不会触发程序中的任何业务逻辑。
The problem is that once WPF updates the value with the property setter, it ignores any further property changed notifications from within that call: it assumes that they will happen as a normal part of the setter and are of no consequence, even if you really have updated the property back to the original value.
The way I got around this was to allow the field to get updated, but also queue up an action on the Dispatcher to "undo" the change. The action would set it back to the old value and fire a property change notification to get WPF to realize that it's not really the new value it thought it was.
Obviously the "undo" action should be set up so it doesn't fire any business logic in your program.
我遇到了同样的问题,是由 UI 线程和出价工作方式引起的。检查此链接: ComboBox 上的 SelectedItem
示例中的结构使用后面的代码,但MVVM 是完全一样的。
I had the same issue, causes by UI thread and the way that biding works. Check the this link: SelectedItem on ComboBox
The structure in the sample uses code behind but the MVVM is exactly the same.
我更喜欢“splintor”的代码示例而不是“AngelWPF”。但他们的方法非常相似。我已经实现了附加行为 CancellableSelectionBehavior,并且它的工作原理如广告所示。也许只是 splintor 示例中的代码更容易插入到我的应用程序中。 AngelWPF 附加行为中的代码引用了需要更多代码更改的 KeyValuePair 类型。
在我的应用程序中,我有一个 ComboBox,其中 DataGrid 中显示的项目基于 ComboBox 中选择的项目。如果用户对 DataGrid 进行了更改,然后在组合框中选择了一个新项目,我将提示用户使用 Yes|NO|Cancel 按钮作为选项保存更改。如果他们按下“取消”,我想忽略他们在组合框中的新选择并保留旧选择。这就像冠军一样!
对于那些一看到 Blend 和 System.Windows.Interactivity 就被吓到的人来说,您不必安装 Microsoft Expression Blend。您可以下载适用于 .NET 4(或 Silverlight)的 Blend SDK。
混合.NET 4 SDK
Blend SDK for Silverlight 4
哦,是的,在我的 XAML,实际上在本例中我使用它作为 Blend 的命名空间声明:
I prefer "splintor's" code sample over "AngelWPF's". Their approaches are fairly similar though. I have implemented the attached behavior, CancellableSelectionBehavior, and it works as advertised. Perhaps it was just that the code in splintor's example was easier to plug into my application. The code in AngelWPF's attached behavior had references to a KeyValuePair Type that would have called for more code alteration.
In my application, I had a ComboBox where the items that are displayed in a DataGrid are based on the item selected in the ComboBox. If the user made changes to the DataGrid, then selected a new item in the ComboBox, I would prompt the user to save changes with Yes|NO|Cancel buttons as options. If they pressed Cancel, I wanted to ignore their new selection in the ComboBox and keep the old selection. This worked like a champ!
For those who frighten away the moment they see references to Blend and System.Windows.Interactivity, you do not have to have Microsoft Expression Blend installed. You can download the Blend SDK for .NET 4 (or Silverlight).
Blend SDK for .NET 4
Blend SDK for Silverlight 4
Oh yeah, in my XAML, I actually use this as my namespace declaration for Blend in this example: