Why is there no IsReadOnlyChanged event on TextBox controls?

隐身守侯 提交于 2019-12-08 07:54:21

问题


I was adding some workaround code to fix the bug outlined in Is this a bug in DotNet 4 WPF Spell Checking?, (When a WPF TextBox changes Enabled, Visible or ReadOnly states, any SpellCheck custom dictionaries get dropped off until you disable and re-enable SpellCheck) and the simplest fix seemed to be to handle the IsVisibleChanged, IsEnabledChanged, and IsReadOnlyChanged events.

Simple, right? Except there is no IsReadOnlyChanged event. Anybody know why and what the best way to trap a change to IsReadOnly in a WPF TextBox would be?


回答1:


You can always follow dependency property change with DependencyPropertyDescriptor.AddValueChanged

DependencyPropertyDescriptor.FromProperty(TextBoxBase.IsReadOnlyProperty)
                            .AddValueChanged(ctrl, OnReadOnlyChanged)



回答2:


Create a custom class and handle OnPropertyChanged event. Sth like this:

public class MyTextBox: TextBox
{
    public MyTextBox() { }
    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        base.OnPropertyChanged(e);
        if (e.Property.ToString() == "IsReadOnly")
        {
            // here you are sure that ContentPropertyhas changed
        }
    }
}


来源:https://stackoverflow.com/questions/13607096/why-is-there-no-isreadonlychanged-event-on-textbox-controls

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