c# - 如何在 XAML 中分配 Control 类型的属性 - 错误

标签 c# .net wpf xaml user-controls

我有一个具有 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/

相关文章:

c# - 如何从 Visual Studio 2012 中的 resharper 测试 session 窗口导航到功能文件?

.net - 为什么关闭 TrackFileAccess 后 Tracker.exe 响应文件仍然出现错误?

c# - WPF 应用程序中的按钮响应较慢

c# - 按名称对 FileSystemInfo[] 排序

使用按钮回发而不是使用 POST 的 C# Webform 文件上传

c# - 非 ui 线程上的过程图像

c# - 在 WPF 中,创建类似于 Outlook 2007 的日历 View 的最佳方法是什么?

c# - 如何检查输入的密码是否等于存储的密码?

c# EasyHook,窗口标题改变

c# - 按 len() 排序,但在 C# 的 linq 中