WPF DataBinding:取消了属性更改 - Combobox未对齐

时间:2010-07-19 16:46:27

标签: c# wpf data-binding inotifypropertychanged

我有一个带有组合框和文本框的WPF表单(两者都是数据绑定到Object的属性)。更改组合框或文本框输入会更新Object的属性,数据绑定将启动并更新UI。问题是,我实现了一种取消更改的方法,这种方式有效,但却搞砸了UI更新。如果我从组合框中进行更改并取消它,组合框不会将选定的值恢复为应该的值(由对象的值绑定)。如果我从文本框进行更改并取消它,文本框和组合框都显示正确的数据,但然后焦点立即被给予组合框(当它应该留在文本框上,因为那是我的最后一个地方它)。我不确定如何在一般方面解决这个问题,因为它决定处理变更事件并验证之后没有取消变更(因为那时数据绑定的意义何在?)......

//User.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace MyTesting
{
    public class User : AbstractEntity
    {
        public User()
        {
            Rankings = new Dictionary<int,string>();

            Rankings.Add(1, "Newbie");
            Rankings.Add(10, "Novice");
            Rankings.Add(25, "Adept User");
            Rankings.Add(50, "Power User");
            Rankings.Add(100, "Admin God");
        }

        public Dictionary<Int32, String> Rankings { get; set; }

        private Int32 _rank;
        public Int32 Rank
        {
            get
            {
                return _rank;
            }
            set
            {
                SetProperty<Int32>("Rank", ref _rank, value);
            }
        }
    }
}


//AbstractEntity.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace MyTesting
{
    public abstract class AbstractEntity : INotifyPropertyChanging, INotifyPropertyChanged
    {
        protected void SetProperty<T>(String propertyName, ref T property, T value)
        {
            if (!Object.Equals(property, value))
            {
                if (OnPropertyChanging(propertyName, property, value))
                {
                    T oldValue = (T)property;
                    property = value;
                    OnPropertyChanged(propertyName, property, value);
                }
            }
        }

        [field: NonSerialized]
        public event PropertyChangingEventHandler PropertyChanging;

        protected virtual Boolean OnPropertyChanging(String propertyName, Object oldValue = null, Object newValue = null)
        {
            CancellablePropertyChangingEventArgs e;

            if ((oldValue != null) || (newValue != null))
                e = new CancellablePropertyChangingEventArgs(propertyName, oldValue, newValue);
            else
                e = new CancellablePropertyChangingEventArgs(propertyName);

            return OnPropertyChanging(e);
        }
        protected virtual Boolean OnPropertyChanging(CancellablePropertyChangingEventArgs e)
        {
            if (PropertyChanging != null)
                PropertyChanging(this, e as PropertyChangingEventArgs);

            return !e.IsCancelled;
        }

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(String propertyName, Object oldValue = null, Object newValue = null)
        {
            ExtendedPropertyChangedEventArgs e;

            if ((oldValue != null) || (newValue != null))
                e = new ExtendedPropertyChangedEventArgs(propertyName, oldValue, newValue);
            else
                e = new ExtendedPropertyChangedEventArgs(propertyName);

            OnPropertyChanged(e);
        }
        protected virtual void OnPropertyChanged(ExtendedPropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e as PropertyChangedEventArgs);
        }
    }

    public class ExtendedPropertyChangedEventArgs : PropertyChangedEventArgs
    {
        public ExtendedPropertyChangedEventArgs(String propertyName)
            : base(propertyName)
        {
        }

        public ExtendedPropertyChangedEventArgs(String propertyName, Object oldValue, Object newValue)
            : base(propertyName)
        {
            OldValue = oldValue;
            NewValue = newValue;
        }

        public Object OldValue { get; private set; }
        public Object NewValue { get; private set; }
    }

    public class CancellablePropertyChangingEventArgs : PropertyChangingEventArgs
    {
        public CancellablePropertyChangingEventArgs(String propertyName, Boolean cancel = false)
            : base(propertyName)
        {
            IsCancelled = cancel;
        }

        public CancellablePropertyChangingEventArgs(String propertyName, Object oldValue, Object newValue, Boolean cancel = false)
            : base(propertyName)
        {
            OldValue = oldValue;
            NewValue = newValue;

            IsCancelled = cancel;
        }

        public Object OldValue { get; private set; }
        public Object NewValue { get; private set; }

        public Boolean IsCancelled { get; set; }
    }
}


<!-- MainWindow.xaml -->
<Window x:Class="ObservableDictionaryBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:MyTesting"
        Title="MainWindow" Height="350" Width="525" Loaded="OnLoaded">

    <Grid>
        <ComboBox x:Name="RankList" Height="23" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="12,12,12,0" />

        <TextBlock Height="23" Width="40" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="13,100,0,0" Text="Rank:" />
        <TextBox x:Name="RankBox" Height="23" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="59,97,12,0" />
    </Grid>
</Window>

//MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyTesting
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            MyUser = new User();

            InitializeComponent();

            MyUser.PropertyChanging += new PropertyChangingEventHandler(MyUser_PropertyChanging);
        }

        private User MyUser { get; set; }

        private Binding RankListBinding { get; set; }
        private Binding RankBinding { get; set; }
        private Binding RankListRankBinding { get; set; }

        private void OnLoaded(object sender, EventArgs e)
        {
            DataContext = MyUser;

            RankListBinding = new Binding("Rankings");
            RankListBinding.Source = MyUser;
            RankList.SetBinding(ComboBox.ItemsSourceProperty, RankListBinding);
            RankList.SelectedValuePath = "Key";
            RankList.DisplayMemberPath = "Value";

            RankBinding = new Binding("Rank");
            RankBinding.Source = MyUser;
            RankBox.SetBinding(TextBox.TextProperty, RankBinding);

            RankListRankBinding = new Binding("Rank");
            RankListRankBinding.Source = MyUser;
            RankList.SetBinding(ComboBox.SelectedValueProperty, RankListRankBinding);
        }

        private void MyUser_PropertyChanging(Object sender, PropertyChangingEventArgs e)
        {
            CancellablePropertyChangingEventArgs ea = e as CancellablePropertyChangingEventArgs;

            String text = String.Format("Would you like to change the property '{0}' from '{1}' to '{2}'?",
                    e.PropertyName,
                    (ea.OldValue == null) ? "<null>" : ea.OldValue.ToString(),
                    (ea.NewValue == null) ? "<null>" : ea.NewValue.ToString()
                    );

            MessageBoxResult result = MessageBox.Show(this, text, "Property Changed",
                MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes);

            if (result == MessageBoxResult.No)
                ea.IsCancelled = true;
        }
    }
}

更新的方法:这修复了绑定,但是当用户尝试更改文本框中的值,然后取消它时,无法解决组合框中的焦点被盗的问题。但是,至少UI在数据绑定值方面匹配。我找到了这个帮助我的link

protected void SetProperty<T>(String propertyName, ref T property, T value)
{
    if (!Object.Equals(property, value))
    {
        bool cancelled = OnPropertyChanging<T>(propertyName, property, value);

        if (cancelled)
        {
            Application.Current.Dispatcher.BeginInvoke(
                new Action(() =>
                {
                    OnPropertyChanged<T>(propertyName);
                }),
                DispatcherPriority.ContextIdle,
                null
            );

            return;
        }

        T originalValue = property;
        property = value;
        OnPropertyChanged(propertyName, originalValue, property);
    }
}

2 个答案:

答案 0 :(得分:1)

这解决了UI显示正确的数据绑定数据...它只是无法修复被盗的焦点问题:

protected void SetProperty<T>(String propertyName, ref T property, T value)
{
    if (!Object.Equals(property, value))
    {
        bool cancelled = OnPropertyChanging<T>(propertyName, property, value);

        if (cancelled)
        {
            Application.Current.Dispatcher.BeginInvoke(
                new Action(() =>
                {
                    OnPropertyChanged<T>(propertyName);
                }),
                DispatcherPriority.ContextIdle,
                null
            );

            return;
        }

        T originalValue = property;
        property = value;
        OnPropertyChanged(propertyName, originalValue, property);
    }
}

答案 1 :(得分:0)

当用户取消属性更改时,您仍应使用旧值发布INotifyPropertyChanged.PropertyChanged。如果您的绑定是双向的,则用户更改的任何控件都将更改回来。

相关问题