Recently myself and my collegue are discussing regarding view binding to visibility property which is in a viewModel.
In one of our project, in order to make a control visible / collapse my colleague binded to a visibility property and the viewModel will set the property to Visibility.Visible / Visibility.Collapse when necessary.
It works, but I want to know is it a good design.
As I am thinking , if the viewModel is setting the property to Visible / collapse, then viewModel knows what view is doing, instead it should contain a bool property and the view should binded to a bool property and use a converter to decide whether to make the element visible / collapse. In which case viewmodel knows nothing about the view .
Example : Showing a TextBlock( Team name ) if the user belongs to a team
private bool _isUserBelongsToTeam;
public bool IsUserBelongsToTeam
{
get { return _isUserBelongsToTeam; }
set { _isUserBelongsToTeam = value; NotifyPropertyChange("IsUserBelongsToTeam"); }
}
XAML:
<TextBlock Text= "Team name" Visibility= "{Binding IsUserBelongsToTeam, Converter={StaticResource BoolToVisibilityConverter}}" />
private bool _isTeamVisible;
public bool IsTeamVisible
{
get { return _isTeamVisible; }
set { _isTeamVisible= value; NotifyPropertyChange("IsTeamVisible"); }
}
XAML:
<TextBlock Text= "Team name" Visibility= "{Binding IsTeamVisible}" />
which way is better to implement?