如何将附加属性添加到组合框?

时间:2014-11-03 10:00:33

标签: c# wpf xaml

枝。我目前有一个附加属性,用于从TextBoxes获取信息并在需要时更改它。但是它仅适用于textBoxes,这意味着用户控件元素(如ComboBoxes和DatePickers)不能与它一起使用。我不完全确定在哪里改变它以使它与它们一起工作。这是下面的课程。

public class TextBoxProperties
{
    public static readonly DependencyProperty IsTextFormattedProperty = DependencyProperty.RegisterAttached("IsTextFormatted", typeof(bool), typeof(TextBoxProperties ), new UIPropertyMetadata(default(bool), OnIsTextFormattedChanged));

    public static bool GetIsTextFormatted(DependencyObject dependencyObject)
    {
        return (bool)dependencyObject.GetValue(IsTextFormattedProperty);
    }

    public static void SetIsTextFormatted(DependencyObject dependencyObject, bool value)
    {
        dependencyObject.SetValue(IsTextFormattedProperty, value);
    }

    public static void OnIsTextFormattedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        TextBox textBox = dependencyObject as TextBox;
        ComboBox comboBox = dependencyObject as ComboBox;

        // This will work fine...
        if (textBox.Name == "firstNameTextBox")
        {
            bool newIsTextFormattedValue = (bool)dependencyPropertyChangedEventArgs.NewValue;
            if (newIsTextFormattedValue) textBox.TextChanged += MyTextBoxChangedHandler;
            else textBox.TextChanged -= MyTextBoxChangedHandler;
        }

        // This on the other hand will not
        if (comboBox.Name == "genderTextBox")
        {
            bool newIsTextFormattedValue = (bool)dependencyPropertyChangedEventArgs.NewValue;
            if (newIsTextFormattedValue) textBox.TextChanged += MyComboBoxChangedHandler;
            else textBox.TextChanged -= MyComboBoxChangedHandler;
        }
    }

    public static void MyTextBoxChangedHandler(object sender, TextChangedEventArgs e)
    {
        // Do what ever needs to be done with text...
    }

    public static void MyComboBoxChangedHandler(object sender, TextChangedEventArgs e)
    {
        // Do what ever needs to be done with text...
    }

使用它时,我只需将其放在视图的xaml中:

<TextBox TextBoxProperties:IsFormatted="True" ... />
<ComboBox TextBoxProperties:IsFormatted="True" ... />

然而,当我将附加属性添加到组合框时,我在错误窗口中得到“对象引用未设置为对象的实例”错误。如果我运行我的应用程序,它只是崩溃,第一次机会异常显示相同的消息。

有关如何使其发挥作用的任何线索?

1 个答案:

答案 0 :(得分:1)

您应该检查textbox是否为null,以及组合框:

TextBox textBox = dependencyObject as TextBox;
    ComboBox comboBox = dependencyObject as ComboBox;

    if (textBox != null && textBox.Name == "firstNameTextBox")
    {
        bool newIsTextFormattedValue = (bool)dependencyPropertyChangedEventArgs.NewValue;
        if (newIsTextFormattedValue) textBox.TextChanged += MyTextChangedHandler;
        else textBox.TextChanged -= MyTextChangedHandler;
    }

    if (comboBox != null && comboBox.Name == "genderTextBox")
    {
        bool newIsTextFormattedValue = (bool)dependencyPropertyChangedEventArgs.NewValue;
        if (newIsTextFormattedValue) comboBox.SomeEvent += MyComboBoxChangedHandler;
        else comboBox.SomeEvent -= MyComboBoxChangedHandler;
    }