Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Micro,MVVM模式:CanExecute命令不';行不通_C#_Wpf_Mvvm_Caliburn.micro - Fatal编程技术网

C# Micro,MVVM模式:CanExecute命令不';行不通

C# Micro,MVVM模式:CanExecute命令不';行不通,c#,wpf,mvvm,caliburn.micro,C#,Wpf,Mvvm,Caliburn.micro,我的看法是: <StackPanel Orientation="Horizontal" VerticalAlignment="Top"> <Label>Customer name:</Label> <TextBox Text="{Binding Customer.Name, UpdateSourceTrigger=PropertyChanged}" Width="136"/> <Button x:Name="Updat

我的看法是:

<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
    <Label>Customer name:</Label>
    <TextBox Text="{Binding Customer.Name, UpdateSourceTrigger=PropertyChanged}" Width="136"/>
    <Button x:Name="UpdateClick">Update</Button>
</StackPanel>
这是我的模型:

private string name;
public string Name
{
    get { return name; }
    set { name = value; NotifyOfPropertyChange(() => Name); }
}

所以我有UpdateClick方法,它工作得很好。我也有CanUpdateClick属性,但它不工作,我不知道为什么?当文本框为空时,应禁用UI上的按钮。请帮忙

您可以订阅
Customer
类的
PropertyChanged
事件(因为您似乎正在对
PropertyChangedBase
进行子类化),并在
Name
属性更改时调用
NotifyOfPropertyChanged(()=>CanUpdateClick)

// in your view model
// i'm assuming here that Customer is set before your view model is activated
protected override void OnActivate()
{
    base.OnActivate();
    Customer.PropertyChanged += CustomerPropertyChangedHandler;
}

protected override void OnDeactivate(bool close)
{
    base.OnDeactivate(close);
    // unregister handler
    Customer.PropertyChanged -= CustomerPropertyChangedHandler;
}

// event handler
protected void CustomerPropertyChangedHandler(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == nameof(Customer.Name))
    {
        NotifyOfPropertyChange(() => CanUpdateClick);
    }
}
或者,您可以在视图模型中创建要绑定的
名称
客户名称
属性,a)使用
客户名称
作为支持字段,或b)使用普通支持字段,然后在更新时只需设置
客户名称

在你看来:

<TextBox Text="{Binding CustomerName, UpdateSourceTrigger=PropertyChanged}" Width="136"/>

这是因为notify属性更改只是通知
Customer.Name
已更改,而不是
CanUpdateClick
。Name是customer类中的一个属性,因此必须点击customer的propertychanged才能获得该通知。谢谢。我更喜欢你的第二个建议。但是我不知道如何在显示验证消息的视图中实现ContentPresenter?
<TextBox Text="{Binding CustomerName, UpdateSourceTrigger=PropertyChanged}" Width="136"/>
public string CustomerName
{
    get { return Customer.Name; }
    set
    {
        Customer.Name = value;
        NotifyOfPropertyChange(); // CallerMemberName goodness
        NotifyOfPropertyChange(() => CanUpdateClick);
    }
}