【问题标题】:Why is there no IsReadOnlyChanged event on TextBox controls?为什么 TextBox 控件上没有 IsReadOnly Changed 事件?
【发布时间】:2012-12-14 21:42:50
【问题描述】:

我正在添加一些解决方法代码来修复Is this a bug in DotNet 4 WPF Spell Checking? 中概述的错误,(当 WPF 文本框更改 Enabled, VisibleReadOnly 状态时,任何 SpellCheck 自定义词典都会被丢弃,直到您禁用并重新启用拼写检查),最简单的修复似乎是处理IsVisibleChangedIsEnabledChangedIsReadOnlyChanged 事件。

很简单,对吧?除了 没有 IsReadOnlyChanged 事件。任何人都知道为什么以及在 WPF 文本框中捕获对 IsReadOnly 的更改的最佳方法是什么?

【问题讨论】:

    标签: wpf textbox event-handling spell-checking readonly


    【解决方案1】:

    您始终可以使用DependencyPropertyDescriptor.AddValueChanged 跟踪依赖项属性更改

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

    【讨论】:

    • +1,但为 OP 的具体问题添加样本将大大增强您的答案。
    • 酷。我认为必须有某种方法来处理这个问题。谢谢!
    【解决方案2】:

    创建一个自定义类并处理 OnPropertyChanged 事件。像这样:

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

    【讨论】:

      猜你喜欢
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2012-08-28
      • 2010-12-12
      相关资源
      最近更新 更多