Best Approach of setting the Visibility in MVVM Best Approach of setting the Visibility in MVVM wpf wpf

Best Approach of setting the Visibility in MVVM


The Best approach in MVVM does not necessarily mean easy. I like the following approaches:

a) Create a boolean for the visibility of each object, and bind each object to this (with a bool->visibility converter).

This method is the most intuitive and classically for setting Visibility for Control.

b) Bind to the enum, with a unique converter for each object.

c) Bind to the enum, with a single converter that takes a parameter.

In the case of the Converter, Enum is the best keep not in the Model and in the side of View. Because the problem solves over to the side of View, which is quite logical and here to store the data structure. In principle, it is not critical.

Example:

public sealed class InvertableBooleanToVisibilityConverter : IValueConverter{    enum Parameters    {        Normal,         Inverted    }    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)    {        var boolValue = (bool)value;        var direction = (Parameters)Enum.Parse(typeof(Parameters), (string)parameter);        if (direction == Parameters.Inverted)            return !boolValue ? Visibility.Visible : Visibility.Collapsed;        return boolValue ? Visibility.Visible : Visibility.Collapsed;    }    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)    {        return DependencyProperty.UnsetValue;    }}

A couple of comments about other approaches:

d) Use a visual state manager with boolean key frames, and drive the state from VM with an attached property.

For these situations, it looks complicated, so do not see the point in it. But, if the conditions of setting Visibility are not difficult, you can use VisualStateManager.

e) Bind to the VM enum from code behind, and set visibility thru code.

Code-behind in this case is not justified when you can solve the problem using typical tools of MVVM (Binding, Converters, etc). I think, in this case it would not be a violation of the principle of MVVM, if choosing the element to the Visibility is not involved business logic, such as may come setting Visibility by pressing of CheckBox, ToggleButton, etc.


c) Bind to the enum, with a single converter that takes a parameter.

This way I solved my problem with radio buttons where only one of the radio buttons is selected. Seem harder but you can reuse it later and if you get the idea it is very easy.

e) Bind to the VM enum from code behind, and set visibility thru code.

Bad idea. That's not a good practice.


in addition to my comment. when i have the task to show diffenrent content in a view depending on different objects. i use most time a contentcontrol and datatemplates.

  <ContentControl Content="{Binding MyEnumObject}"/>

now in my viewmodel i have all the diffrent objects types and set the object i want to my Property bind to the view.

   private Blup _blup; //eg. is wanted when enum in model = 1   public object MyEnumObject {get;set;}   //set the content in your viewmodel    this.MyEnumObject = _blup;

now you just need a datatemplate to visualize your objects

   <DataTemplate DataType="{x:Type local:Blup}">    <view:MyBlupViewControl/>   </DataTemplate>

but maybe i m totally wrong. but your actuall question let so much room for interpretation