75238

Bind two elements' Visibility to one property

Question:

I have two Menu Item elements - "Undelete" and "Delete" who have complementary visibility: when one is shown, the other one is hidden.

In the code of the ViewModel I have a dependency property FilesSelectedCanBeUndeleted defined as below:

private bool _filesSelectedCanBeUndeleted; public bool FilesSelectedCanBeUndeleted { get { return _filesSelectedCanBeUndeleted; } set { _filesSelectedCanBeUndeleted = value; OnPropertyChanged("FilesSelectedCanBeUndeleted"); } }

the XAML for the Undelete button looks like below:

<MenuItem Header="Undelete" Command="{Binding UndeleteCommand }" Visibility="{Binding Path=FilesSelectedCanBeUndeleted, Converter={StaticResource BoolToVisConverter}}" >

As you can see the Visibility of the Undelete is bind to the FilesSelectedCanBeUndeleted property ( with the help of a BooleanToVisibilityConveter).

Now my question is, how can I write the XAML to bind the Visibility of the Delete button to the "NOT" value of the FilesSelectedCanBeUndeleted property?

Thanks,

Answer1:

<a href="http://kentb.blogspot.com/2011/02/booleantovisibilityconverter.html" rel="nofollow">Here</a> is an example of a custom IValueConverter, that allows you to reverse the visibility logic. Basically, one MenuItem will be visible when your view-model property is true, and the other would be collapsed.

So you'd need to define two instances of the converter like so:

<local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /> <local:BooleanToVisibilityConverter x:Key="ReversedBooleanToVisibilityConverter" IsReversed="true" />

Answer2:

You can use apply the datatrigger to you menuitem to avoid another property in your viemodel like this -

<MenuItem Header="Delete" Command="{Binding DeleteCommand }"> <MenuItem.Style> <Style TargetType="{x:Type MenuItem}"> <Setter Property="Visibility" Value="Visible" /> <Style.Triggers> <DataTrigger Binding="{Binding FilesSelectedCanBeUndeleted}" Value="False"> <Setter Property="Visibility" Value="Collapsed" /> </DataTrigger> </Style.Triggers> </Style> </MenuItem.Style> </MenuItem>

Answer3:

Create new property on your ViewModel and just Negate 'FilesSelectedCanBeUndeleted' and then bind to it.

Answer4:

I did something like this a while ago with a simple negation...

private bool _filesSelectedCanBeUndeleted; public bool FilesSelectedCanBeUndeleted{ get{ return _filesSelectedCanBeUndeleted; } set{ _filesSelectedCanBeUndeleted = value; OnPropertyChanged("FilesSelectedCanBeUndeleted"); // You have also to notify that the second Prop will change OnPropertyChanged("FilesSelectedCanBeDeleted"); }} public bool FilesSelectedCanBeDeleted{ get{ return !FilesSelectedCanBeUndeleted; } }

Xaml could look like this then ....

<MenuItem Header="Delete" Command="{Binding DeleteCommand }" Visibility="{Binding Path=FilesSelectedCanBeDeleted, Converter={StaticResource BoolToVisConverter}}" >

Recommend

  • SendAsync (async email) and event handlers / lifecycle in ASP.NET?
  • exporting large files to excel using PHPExcel
  • NewEgg API access for price, inventory (JSON/XML) [closed]
  • How to cancel a Task using CancellationToken?
  • PFSubclassing with array pointer and swift 1.2 - fatal error: NSArray element failed to match the Sw
  • Is a using-declaration supposed to hide an inherited virtual function?
  • Why is this procedure generating two Emails?
  • What is the behavior of document.createElement when passed as an argument?
  • How to store model in `.pb` file with Estimator in TensorFlow?
  • How to parse JSON in Spring Boot Application automatically using Jackson
  • Excel export not working on Firefox but working fine in google crome
  • Rendering equation for Photon Mapping
  • Starting Bootstrap tour with a button after ending tour once
  • SSRS Subscription to an email group (distribution list) is not working
  • Read attribute/value pairs from XML file using Linq
  • How to customize marker colors and shapes in scatter plot?
  • Error: packet sequence number wrong after upgrading RDS instance
  • Python Regex[Forking] - Capture Groups Based on Terms but Skipping if another Term in the set is enc
  • Application backgrounding in Xamarin.Mac
  • JSF with Enum 'Validation Error: Value is not valid'
  • Joining across databases with dbplyr
  • Sprite animation wobbly / jumping in IE11
  • ASP.NET MVC 3 ListBox validation
  • Why clearfsimport command adds file to source control with a size zero?
  • jqGrid Filter Toolbar and Date and Select filters
  • MVC3 Extension for ValidatorMessage
  • async GET request with body from browser
  • can you use embedded ruby in custom javascript files in rails?
  • Failed to resolve: firebase-auth-15.0.0 [closed]
  • I need a PHP mySQL Login script that directs user to specific URL based on username and password
  • Sample deviceQuery cuda program
  • Java Collections.shuffle() weird behaviour [closed]
  • how to specify different css for ie
  • How can i move Clearcase dyamic/snapshot views to another host (Linux)
  • trigger ontouch event programmatically
  • When to use the tag in the head and body section of a html page? [duplicate]
  • Create an average of multiple excel chart without the data source
  • How to use Streams api peek() function and make it work?
  • Google App Engine backend servlet not responding