CanExecute Caliburn 方法不适用于嵌套属性
CanExecute Caliburn method not working with nested properties
我正在尝试使用 Caliburn Micro CanExecute 方法根据是否在文本框中输入值来绑定我的保存按钮是否被禁用。
这是我的看法:
<TextBox Text="{Binding Current.Name}"/>
<Button Content="Save" Name="Save" />
我的POCO:
[ImplementPropertyChanged] (fody)
public partial class POCO: IEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
还有我的 ViewModel:
public void SetupViewModel()
{
Current = new POCO();
}
public POCO Current { get; set; }
public bool CanSave => Current == null || !string.IsNullOrWhiteSpace(Current?.Name);
如果我设置一个变量并将其绑定到视图中(让我们称之为 POCOName
)并将其传递给 CanSave
它工作正常,似乎只是嵌套属性的问题.有人有什么想法吗?
动作约定功能使用 x:Name 指令而不是名称 属性。
<TextBox Text="{Binding Current.Name}"/>
<Button Content="Save" x:Name="Save" />
更新
我已经尝试过使用 Name 属性 它也可以,很抱歉给了你错误的信息。
CanExcute 方法在你的例子中实际上是一个属性,你需要通知属性 改变了,例如。
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyOfPropertyChange(nameof(Name));
// when Name changed, check if it is null or whitespace
NotifyOfPropertyChange(nameof(CanSave));
}
}
这取决于您的应用程序在哪里通知 CanExcute 属性 已更改。
也可以使用CanExcute方法,每次参数值改变时都会调用。
有关 Caliburn Micro Action 的更多信息,请参阅 All About Actions
我正在尝试使用 Caliburn Micro CanExecute 方法根据是否在文本框中输入值来绑定我的保存按钮是否被禁用。
这是我的看法:
<TextBox Text="{Binding Current.Name}"/>
<Button Content="Save" Name="Save" />
我的POCO:
[ImplementPropertyChanged] (fody)
public partial class POCO: IEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
还有我的 ViewModel:
public void SetupViewModel()
{
Current = new POCO();
}
public POCO Current { get; set; }
public bool CanSave => Current == null || !string.IsNullOrWhiteSpace(Current?.Name);
如果我设置一个变量并将其绑定到视图中(让我们称之为 POCOName
)并将其传递给 CanSave
它工作正常,似乎只是嵌套属性的问题.有人有什么想法吗?
动作约定功能使用 x:Name 指令而不是名称 属性。
<TextBox Text="{Binding Current.Name}"/>
<Button Content="Save" x:Name="Save" />
更新
我已经尝试过使用 Name 属性 它也可以,很抱歉给了你错误的信息。
CanExcute 方法在你的例子中实际上是一个属性,你需要通知属性 改变了,例如。
public string Name
{
get { return _name; }
set
{
_name = value;
NotifyOfPropertyChange(nameof(Name));
// when Name changed, check if it is null or whitespace
NotifyOfPropertyChange(nameof(CanSave));
}
}
这取决于您的应用程序在哪里通知 CanExcute 属性 已更改。
也可以使用CanExcute方法,每次参数值改变时都会调用。
有关 Caliburn Micro Action 的更多信息,请参阅 All About Actions