我有一个具有 Control 类型属性的 WPF 用户控件,将其与它将向其发送按键操作的另一个控件(通常是文本框)相关联。 我可以在 XAML 中分配此属性,它会在运行时编译并运行。
但是 IDE 显示错误“38 方法或操作未实现”。关于 TargetControl 分配。
<my:NumericButtonsControl
x:Name="NumericButtons"
TargetControl="{x:Reference Name=DataEntryTextBox}" />
有问题的用户控制代码如下所示:
public partial class NumericButtonsControl : UserControl
{
private Control _TargetControl;
public Control TargetControl
{
get
{
return _TargetControl;
}
set
{
_TargetControl = value;
}
}
}
最佳答案
当您编写 WPF 应用程序时,您可能希望避免使用 x:Reference
,因为它是 XAML 2009 的一项功能,仅适用于松散的 XAML。
或者,您可以使用绑定(bind)及其 ElementName
属性。但要使其工作,您需要将 TargetControl
设为依赖属性。然后它会看起来像这样:
C#
public Control TargetControl
{
get { return (Control)this.GetValue(TargetControlProperty); }
set { this.SetValue(TargetControlProperty, value); }
}
public static readonly DependencyProperty TargetControlProperty =
DependencyProperty.Register("TargetControl",
typeof(Control),
typeof(NumericButtonsControl),
new PropertyMetadata(null));
XAML:
<my:NumericButtonsControl
x:Name="NumericButtons"
TargetControl="{Binding ElementName=DataEntryTextBox}" />
关于c# - 如何在 XAML 中分配 Control 类型的属性 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17643671/