无法绑定到我的自定义控件中的命令
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 information: Failed to assign to property
'TestApp.Helpers.DelayedTextBox.DelayedTextChangedCommand'. [Line: 29
Position: 103] An exception of type
'Windows.UI.Xaml.Markup.XamlParseException' occurred in TestApp.exe
but was not handled in user code WinRT information: Failed to assign
to property
'TestApp.Helpers.DelayedTextBox.DelayedTextChangedCommand'. [Line: 29
Position: 103] The text associated with this error code could not be
found.
Failed to assign to property
'TestApp.Helpers.DelayedTextBox.DelayedTextChangedCommand'. [Line: 29
Position: 103]
我非常困惑为什么它不能正确绑定,我做错了什么?
问题是我没有像@Lynn Crumbling 在我的问题的评论中提到的那样添加 dependencyProperty。
将以下内容添加到 DelayedTextBox 就可以了:
public static readonly DependencyProperty DelayedTextChangedCommandProperty =
DependencyProperty.Register(
"DelayedTextChangedCommand",
typeof(ICommand),
typeof(DelayedTextBox),
new PropertyMetadata(0));
我创建了一个基于 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 information: Failed to assign to property 'TestApp.Helpers.DelayedTextBox.DelayedTextChangedCommand'. [Line: 29 Position: 103] An exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred in TestApp.exe but was not handled in user code WinRT information: Failed to assign to property 'TestApp.Helpers.DelayedTextBox.DelayedTextChangedCommand'. [Line: 29 Position: 103] The text associated with this error code could not be found.
Failed to assign to property 'TestApp.Helpers.DelayedTextBox.DelayedTextChangedCommand'. [Line: 29 Position: 103]
我非常困惑为什么它不能正确绑定,我做错了什么?
问题是我没有像@Lynn Crumbling 在我的问题的评论中提到的那样添加 dependencyProperty。
将以下内容添加到 DelayedTextBox 就可以了:
public static readonly DependencyProperty DelayedTextChangedCommandProperty =
DependencyProperty.Register(
"DelayedTextChangedCommand",
typeof(ICommand),
typeof(DelayedTextBox),
new PropertyMetadata(0));