根据状态禁用多个控件 [英] Disabling multiple controls depending on a state

查看:46
本文介绍了根据状态禁用多个控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Caliburn和WPF的新手,请问这是一个非常琐碎的问题。

I'm new to both Caliburn and WPF, so excuse me if it is a rather trivial question.

情况如下:
我具有多个控件(例如按钮和文本框-后者是重要的部分)。
其状态(启用/禁用)取决于布尔属性。

The scenario is the following: I have multiple controls (like buttons and textboxes - the latter is the important part). Their state (Enabled/Disabled) are dependent on a boolean property.

我尝试使用的第一个建议方法是使用Can [FunctionName]约定和NotifyOfPropertyChange( ()=> Can [FunctionName])。

The first suggested method I tried was using the Can[FunctionName] convention and NotifyOfPropertyChange(() => Can[FunctionName]). It worked well with the button, but it did not work with the textbox.

如何将IsEnabled属性绑定到状态而不使用View的代码?

How do I bind IsEnabled property to a state without using the code-behind of the View?

我在ViewModel中尝试的代码不适用于文本框:

The code I tried in the ViewModel didn't work for the textbox:

private bool _buttonEnableState = true;

public bool ButtonEnableState
{
    get
    {
        return _buttonEnableState;
    }

    set
    {
        _buttonEnableState = value;

        NotifyOfPropertyChange(() => CanTheButton);
        NotifyOfPropertyChange(() => CanTheTextBox);
    }
}

public bool CanTheButton
{
    get
    {
        return ButtonEnableState;
    }
}

public void TheButton()
{
}

public bool CanTheTextBox
{
    get
    {
        return ButtonEnableState;
    }
}

从视图中:

<Button x:Name="TheButton" Content="This is the button" ... />
<TextBox x:Name="TheTextBox" ... />

预先感谢!

推荐答案

您是否尝试过显而易见的操作?:

Have you tried the obvious?:

<Button Content="This is the button" IsEnabled="{Binding ButtonEnableState}" />
<TextBox x:Name="TheTextBox" IsEnabled="{Binding ButtonEnableState}" />

更新>>>

继续从评论中进行对话...现在,您在 AppViewModel 类中有一个 public 属性,并且它的一个实例类设置为包含 Button TextBox DataContext c $ c>控件?

So, continuing the conversation from the comments... now you have a public property in your AppViewModel class and an instance of that class is set as the DataContext of your view that contains the Button and TextBox controls?

让我们看看 Binding 是否确实在工作?不...尝试将您的代码更改为:

Let's see if the Binding is really working or not... try changing your code to this:

<Button Content="{Binding ButtonEnableState}" />

如果设置了 Button.Content Binding 可以正常工作,并且您有其他问题。

If the Button.Content is set then the Binding works just fine and you have a different problem.

UPDATE 2 >>>

UPDATE 2 >>>

正如@Charleh所述,您还需要确保已将属性值的更改通知 INotifyPropertyChanged 接口:

As @Charleh mentioned, you also need to make sure that you have notified the INotifyPropertyChanged interface of the change of property value:

NotifyOfPropertyChange(() => ButtonEnableState);

这篇关于根据状态禁用多个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆