WPF DataBinding: Cancelled property change - Combobox misaligns

[亡魂溺海] 提交于 2019-12-05 11:27:24

This solves the UI displaying proper databound data ... it just doesn't fix the stolen focus issue:

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);
    }
}

When the user cancels a property change you should still post the INotifyPropertyChanged.PropertyChanged with the old value. If your bindings are twoway any control that been changed by the user will change back.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!