我是新来的都卡利和WPF,所以原谅我,如果这是一个相当琐碎的问题。
该方案是这样的:我有多个控件(如按钮和文本框 - 后者是重要组成部分)。 他们的状态(启用/禁用)都依赖于一个布尔属性。
第一建议的方法我试图用的是可以[FunctionName]惯例和NotifyOfPropertyChange(()=>可以[FunctionName])。 它与按钮效果不错,但它并没有与文本的工作。
如何IsEnabled属性绑定到一个状态,而无需使用视图的代码隐藏?
我在尝试视图模型的代码没有为文本框的工作:
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" ... />
提前致谢!
你有没有试过?:明显
<Button Content="This is the button" IsEnabled="{Binding ButtonEnableState}" />
<TextBox x:Name="TheTextBox" IsEnabled="{Binding ButtonEnableState}" />
UPDATE >>>
所以,从注释中继续对话......现在你有一个public
你的财产AppViewModel
类和类的实例设置为DataContext
视图中包含Button
和TextBox
控件?
让我们来看看,如果Binding
真的工作或不...试着改变你的代码如下:
<Button Content="{Binding ButtonEnableState}" />
如果Button.Content
设置,则Binding
的作品就好了,你有一个不同的问题。
更新2 >>>
作为@Charleh提到的,你还需要确保你已经通知了INotifyPropertyChanged
属性值的变化的界面:
NotifyOfPropertyChange(() => ButtonEnableState);
我不认为我要建议什么必然是做事的正确方法,但它可能给你,你之后的结果。
为了获得控制被禁用根据Can<name>
属性,你需要确认该卡利使用,所以在这种情况下的约定,提供一个功能<name>
应工作:
public void TheTextBox()
{
}
作为默认约定的结果,我相信这都会被调用时KeyDown
事件。
这就是说,你可能希望你的文本内容绑定到的东西,你会想用x:Name
属性约定来选择哪个属性,这意味着你必须附上TheTextBox()
函数以不同的方式,你应该能够在使用该做Message.Attach
在属性Caliburn
命名空间。
所以,你的文本框看起来是这样的(在这里您已经添加以下命名空间xmlns:cal="http://www.caliburnproject.org"
):
<TextBox cal:Message.Attach="TheTextBox" Name="SomeTextProperty" />
支持那些在你的视图模型,你必须:
// Your Enabled Property (using your existing code).
public bool CanTheTextBox
{
get
{
return ButtonEnableState;
}
}
// Your dummy function
public void TheTextBox()
{
}
// Some text property (Just for demo, you'd probably want to have more complex logic in the get/set
public string SomeTextProperty
{
get; set;
}
然后,您应该看到启用/禁用行为,并使用SomeTextProperty
。
我不能完全确定我喜欢做的事情的这种方式,我只是有一个快速的打法,看看它是否工作。 以下的答案可能是一个清洁的解决方案,并建立了一个新的可重复使用的约定:
添加一个约定的IsEnabled到Caliburn.Micro
作为一个轻微的一边(而不是直接的答案),这取决于你的控制/表格是多么复杂,你可以调查使用多Views
为同一ViewModel
,在过去,我已经设置了ReadOnly
和Editable
视图,并使用单在属性ViewModel
的两个之间进行切换(基本上设置的整个状态ViewModel
)。 目前已经有默认的惯例,所以你可以使用相对容易多了意见。