简体   繁体   English

将控件/ UIElement可见性正确正确地绑定到属性MVVM C#WPF

[英]Correct and proper binding of a control/UIElement visibility to Property MVVM C# WPF

I'm quite new to MVVM, and I've been constructing my ViewModels. 我对MVVM还是很陌生,并且一直在构建ViewModel。 I have a ViewModel which contains an ICommand , which is then bound to in my View by a command button. 我有一个包含ICommand的ViewModel,然后通过命令按钮将其绑定到我的View中。 The ICommand causes a procedure to be invoked on my ViewModel which then invokes a further large slow procedure. ICommand导致在我的ViewModel上调用一个过程,然后再调用一个更大的慢速过程。 While this procedure is happening I want to make a control/ UIElement 's visibility to become visible and then hidden after the procedure has finished (I intend to bind a label and indeterminate progress bar's visibility) 在执行此过程时,我想使Control / UIElement的可见性变得可见,然后在该过程完成后将其隐藏(我打算绑定标签并不确定进度条的可见性)

For example, in my view model I have 例如,在我的视图模型中

public void calledFromCommandButton() {
RaisePropertyChange("Starting");
superLongProcedure();
RaisePropertyChange("Finished");

}

This just feels a bit silly though, having to raise 2 different property changes and hence, I presume I'm doing it all wrong. 但是,这感觉有点愚蠢,必须提出两个不同的属性更改,因此,我想我做错了所有事情。 I think I could do it with one property change along with a convertor? 我想可以通过一个属性更改和一个转换器来做到这一点?

So, what is the proper and correct method to bind UIElement visibilities to property change events? 那么,将UIElement可见性绑定到属性更改事件的正确和正确方法是什么?

Thanks Thomas 谢谢托马斯

I would recommend using a single boolean property (IsWorking or something) and then using the BooleanToVisibilityConverter to show and hide the button. 我建议使用单个boolean属性(IsWorking或其他),然后使用BooleanToVisibilityConverter来显示和隐藏按钮。 So, it would look something like: 因此,它看起来像:

<Window ...>
    <Window.Resources>
       <BooleanToVisibilityConverter x:Key="TrueToVisibleConverter"/>
    </Window.Resources>
     ...
    <Button x:Name="CancelButton" Content="Cancel" Visiblity="{Binding IsWorking, Converter={StaticResource TrueToVisibleConverter}}"/>
     ...
</Window/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM