问题
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