繁体   English   中英

无法绑定到我的自定义控件中的命令

[英]Can't bind to a command in my custom control

我创建了一个基于TextBox的控件,如下所示:

public class DelayedTextBox : TextBox
{
    public ICommand DelayedTextChangedCommand
    {
        get;
        set;
    }
}

然后,我使用以下代码创建Foo.xaml:

<h:DelayedTextBox DelayedTextChangedCommand="{Binding FooCommand}"/>

我有FooViewModel(它在XAML中正确连接,因为其他部分可以绑定到FooViewModel中的属性)具有:

private ICommand _fooCommand;
    public ICommand FooCommand
    {
        get
        {
            if (_fooCommand == null)
            {
                _fooCommand = new RelayCommand(CheckFoo, () => CanExecuteFooCheck());
            }

            return _fooCommand;
        }
    }

我正在使用GalaSoft.MvvmLight的RelayCommand,它适用于我项目中的其他命令

当我运行我的项目时,我在加载Foo.xaml时收到以下错误:

WinRT信息:无法分配给属性'TestApp.Helpers.DelayedTextBox.DelayedTextChangedCommand'。 [行:29位置:103] TestApp.exe中出现“Windows.UI.Xaml.Markup.XamlParseException”类型的异常,但未在用户代码WinRT信息中处理:无法分配给属性'TestApp.Helpers.DelayedTextBox。 DelayedTextChangedCommand”。 [行:29位置:103]找不到与此错误代码关联的文本。

无法分配给属性'TestApp.Helpers.DelayedTextBox.DelayedTextChangedCommand'。 [线路:29位置:103]

我非常困惑为什么它不能正确绑定,我做错了什么?

问题是我没有在我的问题的评论中提到的@Lynn Crumbling中添加了dependencyProperty。

将以下内容添加到DelayedTextBox就可以了:

public static readonly DependencyProperty DelayedTextChangedCommandProperty =
    DependencyProperty.Register(
        "DelayedTextChangedCommand",
        typeof(ICommand),
        typeof(DelayedTextBox),
        new PropertyMetadata(0));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM