[英]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.