简体   繁体   中英

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. I have a ViewModel which contains an ICommand , which is then bound to in my View by a command button. The ICommand causes a procedure to be invoked on my ViewModel which then invokes a further large slow procedure. 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)

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?

Thanks Thomas

I would recommend using a single boolean property (IsWorking or something) and then using the BooleanToVisibilityConverter to show and hide the button. 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/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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