Travyguy9
Travyguy9

Reputation: 4344

Have two controls set the visibility of another control

Sorry for the title, I just don't know how to explain it in one sentence.

So here is my goal: I need to have a boolean in my ViewModel define the visibility for a control (border).

I know I can achieve this with a BooleanToVisibilityConverter, but there is a little more to it. I want a button on my UI to be shown if the control is not visible. Once that button is pushed, then I want the boolean in my ViewModel to be TRUE and then I want the control to be visible and the button that was just pushed to be collapsed. Once that control is visible, I would like a button within that recently visible control to make the control collapsed and then make the original button visible.

Basically, there are two buttons: 1 to make visible (then collapse itself) and the other is to collapse its container and then make the first button visible.

I am trying to do all this with MVVM so if I can avoid code behind in my View that would be ideal!

Upvotes: 1

Views: 1485

Answers (2)

Thelonias
Thelonias

Reputation: 2935

Since you're using ICommands on your viewmodel, this should work...Assume your commands are "ShowBorderCommand" and "HideBorderCommand" and the property on your viewmodel is "ShowBorder"

<ConverterNamespace:BooleanToVisibilityConverter x:Key="BoolToVis"/>
<ConverterNamespace:ReverseBooleanToVisibilityConverter x:Key="BoolToCollapse"/>

<Border Visibility="{Binding ShowBorder, Converter={StaticResource BoolToVis}}">
    <Button Command="{Binding HideBorderCommand}"/>
</Border>

<Button Command="{Binding ShowBorderCommand}" Visbility="{Binding ShowBorder, Converter={StaticResource BoolToCollapse}}"/>

Upvotes: 1

Kent Boogaart
Kent Boogaart

Reputation: 178630

My WPF Converters library has a BooleanToVisibilityConverter that allows reverse conversions, as well as allowing the use of Hidden instead of Collapsed:

<con:BooleanToVisibilityConverter x:Key="ReverseBooleanToVisibilityConverter" IsReversed="True"/>
<Button Visibility="{Binding SomeProperty, Converter={StaticResource ReverseBooleanToVisibilityConverter}}"/>

Upvotes: 1

Related Questions