Cancel combobox selection in WPF with MVVM

后端 未结 12 1449
你的背包
你的背包 2020-12-08 10:23

I\'ve got a combobox in my WPF application:



        
相关标签:
12条回答
  • 2020-12-08 10:45

    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:

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    
    0 讨论(0)
  • 2020-12-08 10:45

    I did it in a similar way to what splintor has above.

    Your view:

    <ComboBox  
    ItemsSource="{Binding CompetitorBrands}" 
    DisplayMemberPath="Value" 
    SelectedValuePath="Key" 
    SelectedValue="{Binding Path=CompMfgBrandID, 
    Mode=TwoWay,
    UpdateSourceTrigger=Explicit}" //to indicate that you will call UpdateSource() manually to get the property "CompMfgBrandID" udpated 
    SelectionChanged="ComboBox_SelectionChanged"  //To fire the event from the code behind the view
    Text="{Binding CompMFGText}"/>
    

    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

    private int previousSelection = 0; //Give it a default selection value
    
    private bool promptUser true; //to be replaced with your own property which will indicates whether you want to show the messagebox or not.
    
    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                ComboBox comboBox = (ComboBox) sender;
                BindingExpression be = comboBox.GetBindingExpression(ComboBox.SelectedValueProperty);
    
                if (comboBox.SelectedValue != null && comboBox.SelectedIndex != previousSelection)
                {
                    if (promptUser) //if you want to show the messagebox..
                    {
                        string msg = "Click Yes to leave previous selection, click No to stay with your selection.";
                        if (MessageBox.Show(msg, "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) //User want to go with the newest selection
                        {
    
                            be.UpdateSource(); //Update the property,so your ViewModel will continue to do something
                            previousSelection = (int)comboBox.SelectedIndex;  
                        }
                        else //User have clicked No to cancel the selection
                        {
                            comboBox.SelectedIndex = previousSelection; //roll back the combobox's selection to previous one
                        }
                    }
                    else //if don't want to show the messagebox, then you just have to update the property as normal.
                    {
                        be.UpdateSource();
                        previousSelection = (int)comboBox.SelectedIndex;
                    }
                }
            }
    
    0 讨论(0)
  • 2020-12-08 10:49

    Very simple solution for .NET 4.5.1+:

    <ComboBox SelectedItem="{Binding SelectedItem, Delay=10}" ItemsSource="{Binding Items}"  />
    

    It's works for me in all cases. You can rollback selection in combobox, just fire NotifyPropertyChanged without value assignment.

    0 讨论(0)
  • 2020-12-08 10:55

    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.

    0 讨论(0)
  • 2020-12-08 10:57

    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's SelectedItem 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's SelectedItem changes, it tries to assign it to the its own SelectedItem property. If the assignment fails (probably because the bound VM proeprty setter rejected the assignment), the behavior updates the Selector’s SelectedItem with the current value of its SelectedItem 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:

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Interactivity;
    
    namespace MySampleApp
    {
        internal class CancellableSelectionBehavior : Behavior<Selector>
        {
            protected override void OnAttached()
            {
                base.OnAttached();
                AssociatedObject.SelectionChanged += OnSelectionChanged;
            }
    
            protected override void OnDetaching()
            {
                base.OnDetaching();
                AssociatedObject.SelectionChanged -= OnSelectionChanged;
            }
    
            public static readonly DependencyProperty SelectedItemProperty =
                DependencyProperty.Register("SelectedItem", typeof(object), typeof(CancellableSelectionBehavior),
                    new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedItemChanged));
    
            public object SelectedItem
            {
                get { return GetValue(SelectedItemProperty); }
                set { SetValue(SelectedItemProperty, value); }
            }
    
            public static readonly DependencyProperty IgnoreNullSelectionProperty =
                DependencyProperty.Register("IgnoreNullSelection", typeof(bool), typeof(CancellableSelectionBehavior), new PropertyMetadata(true));
    
            /// <summary>
            /// Determines whether null selection (which usually occurs since the combobox is rebuilt or its list is refreshed) should be ignored.
            /// True by default.
            /// </summary>
            public bool IgnoreNullSelection
            {
                get { return (bool)GetValue(IgnoreNullSelectionProperty); }
                set { SetValue(IgnoreNullSelectionProperty, value); }
            }
    
            /// <summary>
            /// Called when the SelectedItem dependency property is changed.
            /// Updates the associated selector's SelectedItem with the new value.
            /// </summary>
            private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var behavior = (CancellableSelectionBehavior)d;
    
                // OnSelectedItemChanged can be raised before AssociatedObject is assigned
                if (behavior.AssociatedObject == null)
                {
                    System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                    {
                        var selector = behavior.AssociatedObject;
                        selector.SelectedValue = e.NewValue;
                    }));
                }
                else
                {
                    var selector = behavior.AssociatedObject;
                    selector.SelectedValue = e.NewValue;
                }
            }
    
            /// <summary>
            /// Called when the associated selector's selection is changed.
            /// Tries to assign it to the <see cref="SelectedItem"/> property.
            /// If it fails, updates the selector's with  <see cref="SelectedItem"/> property's current value.
            /// </summary>
            private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (IgnoreNullSelection && (e.AddedItems == null || e.AddedItems.Count == 0)) return;
                SelectedItem = AssociatedObject.SelectedItem;
                if (SelectedItem != AssociatedObject.SelectedItem)
                {
                    AssociatedObject.SelectedItem = SelectedItem;
                }
            }
        }
    }
    

    This is the way to use it in XAML:

    <Window x:Class="MySampleApp.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="My Smaple App" Height="350" Width="525"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:local="clr-namespace:MySampleApp"
            xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
            mc:Ignorable="d"
            d:DataContext="{d:DesignInstance local:MainWindowViewModel}">
        <StackPanel>
            <ComboBox ItemsSource="{Binding Options}">
                <i:Interaction.Behaviors>
                    <local:CancellableSelectionBehavior SelectedItem="{Binding Selected}" />
                </i:Interaction.Behaviors>
            </ComboBox>
        </StackPanel>
    </Window>
    

    and this is a sample of the VM property:

    private string _selected;
    
    public string Selected
    {
        get { return _selected; }
        set
        {
            if (IsValidForSelection(value))
            {
                _selected = value;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 10:59

    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 in e.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

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using System.Windows.Interactivity;
    
    namespace MyApp
    {
        internal class CancellableSelectionBehaviour : Behavior<Selector>
        {
            protected override void OnAttached()
            {
                base.OnAttached();
    
                if (MustPerfomInitialChange)
                {
                    OnSelectedItemChanged(this, InitialChangeEvent);
                    MustPerfomInitialChange = false;
                }
    
                AssociatedObject.SelectionChanged += OnSelectionChanged;
            }
    
            protected override void OnDetaching()
            {
                base.OnDetaching();
    
                AssociatedObject.SelectionChanged -= OnSelectionChanged;
            }
    
            public static readonly DependencyProperty SelectedItemProperty =
                DependencyProperty.Register("SelectedItem", typeof(object), typeof(CancellableSelectionBehaviour),
                    new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedItemChanged));
    
            public object SelectedItem
            {
                get { return GetValue(SelectedItemProperty); }
                set { SetValue(SelectedItemProperty, value); }
            }
    
            public static readonly DependencyProperty IgnoreNullSelectionProperty =
                DependencyProperty.Register("IgnoreNullSelection", typeof(bool), typeof(CancellableSelectionBehaviour), new PropertyMetadata(true));
    
            /// <summary>
            /// Determines whether null selection (which usually occurs since the combobox is rebuilt or its list is refreshed) should be ignored.
            /// True by default.
            /// </summary>
            public bool IgnoreNullSelection
            {
                get { return (bool)GetValue(IgnoreNullSelectionProperty); }
                set { SetValue(IgnoreNullSelectionProperty, value); }
            }
    
            /// <summary>
            /// OnSelectedItemChanged can be raised before AssociatedObject is assigned so we must delay the initial change. 
            /// Using System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke has unwanted side effects.
            /// So we use this bool to know if OnSelectedItemChanged must be called afterwards, in OnAttached
            /// </summary>
            private bool MustPerfomInitialChange { get; set; }
    
            /// <summary>
            /// OnSelectedItemChanged can be raised before AssociatedObject is assigned so we must delay the initial change. 
            /// Using System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke has unwanted side effects.
            /// So we use this DependencyPropertyChangedEventArgs to save the argument needed to call OnSelectedItemChanged.
            /// </summary>
            private DependencyPropertyChangedEventArgs InitialChangeEvent { get; set; }
    
            /// <summary>
            /// Called when the SelectedItem dependency property is changed.
            /// Updates the associated selector's SelectedItem with the new value.
            /// </summary>
            private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var behavior = (CancellableSelectionBehaviour)d;
    
                // OnSelectedItemChanged can be raised before AssociatedObject is assigned so we must delay the initial change.
                if (behavior.AssociatedObject == null)
                {
                    behavior.InitialChangeEvent = e;
                    behavior.MustPerfomInitialChange = true;               
                }
                else
                {
                    var selector = behavior.AssociatedObject;
                    selector.SelectedValue = e.NewValue;               
                }
            }
    
            /// <summary>
            /// Called when the associated selector's selection is changed.
            /// Tries to assign it to the <see cref="SelectedItem"/> property.
            /// If it fails, updates the selector's with  <see cref="SelectedItem"/> property's current value.
            /// </summary>
            private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (IgnoreNullSelection && (e.AddedItems == null || e.AddedItems.Count == 0)) return;
                SelectedItem = AssociatedObject.SelectedItem;
                if (SelectedItem != AssociatedObject.SelectedItem)
                {
                    AssociatedObject.SelectedItem = SelectedItem;
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题