Why is there no IsReadOnlyChanged event on TextBox

2019-05-30 09:00发布

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?

2条回答
相关推荐>>
2楼-- · 2019-05-30 09:55

You can always follow dependency property change with DependencyPropertyDescriptor.AddValueChanged

DependencyPropertyDescriptor.FromProperty(TextBoxBase.IsReadOnlyProperty)
                            .AddValueChanged(ctrl, OnReadOnlyChanged)
查看更多
劳资没心,怎么记你
3楼-- · 2019-05-30 09:57

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
        }
    }
}
查看更多
登录 后发表回答