【发布时间】:2009-08-25 15:47:12
【问题描述】:
我有一个带有文本框的用户控件。我试图通过在 UserControl 中实现具有相同名称的 DependencyProperty 来公开 texbox 的 Text 属性。因此:
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text",
typeof(string),
typeof(UserControlWithTextBox),
new UIPropertyMetadata(string.Empty));
public string Text
{
get { return (string)GetValue(TextProperty); }
set
{
SetValue(TextProperty, value);
textBox.Text = value;
}
}
get 部分在我的应用程序中似乎可以正常工作。但是,我尝试使用转换器将按钮的 IsEnabled 属性绑定到其中两个 UserControls 的 Text 属性,该转换器将检查两个 UserControls 的 Text 属性是否都是空字符串。应用程序窗口加载时出现以下错误:
System.InvalidCastException 未处理 Message="无法将 MS.Internal.NamedObject 类型的对象转换为 System.String 类型。"
Convert 方法如下所示:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return (string) values[0] != string.Empty &&
(string) values[1] != string.Empty;
}
在抛出异常时,values[0] 和 values[1] 的值都为 DependencyProperty.UnsetValue。
我哪里出错了?
【问题讨论】:
标签: c# .net wpf user-controls dependency-properties