C# 如何将附加属性添加到组合框?
嗨。我现在有一个附加属性,用来从文本框中获取信息,并在需要时对其进行修改。但是,它只适用于文本框,这意味着用户控件元素(如组合框和日期选择器)不能与它一起使用。我不完全确定在哪里修改它,让它也与他们一起工作。这是下面的课程C# 如何将附加属性添加到组合框?,c#,wpf,xaml,C#,Wpf,Xaml,嗨。我现在有一个附加属性,用来从文本框中获取信息,并在需要时对其进行修改。但是,它只适用于文本框,这意味着用户控件元素(如组合框和日期选择器)不能与它一起使用。我不完全确定在哪里修改它,让它也与他们一起工作。这是下面的课程 public class TextBoxProperties { public static readonly DependencyProperty IsTextFormattedProperty = DependencyProperty.RegisterAttach
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" ... />
然而,当我将附加属性添加到组合框时,在错误窗口中会出现“对象引用未设置为对象实例”错误。如果我运行我的应用程序,它只会崩溃,第一次出现的异常会显示相同的消息
关于如何使其工作的任何线索?您应该检查textbo是否为空,以及combobox:
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;
}
如果(comboBox!=null)和@Offer,那么一个对象如何同时是
comboBox
和TextBox
?您应该检查textBox
或comboBox
是否不为null,如果comboBox
不为null,则textBox
将成为参数dependencyObject
只能是一种类型,因此如果使用as
进行强制转换,则必须首先检查类型,确保结果不为空。若要使用TextChanged
事件,可以将dependencyObject
强制转换为Control
,然后可以检查事件处理程序本身中的控件类型。您的名称
检查是多余的,请仅将附加属性分配给将与其一起使用的控件。使用as
后,请始终检查null
,特别是如果您知道它将是不同的类型,否则请使用Control Control=(Control)dependencyObject
(这假设您只将属性附加到控件,而不将属性附加到非控件的内容。@提供您希望为组合框实现的功能?当IsFormatted
对于ComboBox
为真时会发生什么?它仍然会导致空引用异常,因为如果它是ComboBox
,那么您在第二个if中引用的textBox
,将为空。它不能同时是组合框
和文本框
,这不应导致空引用异常,在访问它之前,会检查每个框是否为空。它会。第二,如果在ComboBox
时,没有textBox.TextChanged+=mycomboxchangedhandler
或textBox.TextChanged-=mycomboxchangedhandler
和textBox
,则在本例中为空