Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For your specific example of hiding elements in the view, you probably want to set up some properties in the ViewModel that define the conditions under which those elements are visible. Then you bind the Visibility property (with a BooleanToVisibilityConverter, most likely) of those elements in the View to those properties in the ViewModel.</p> <p>More generally, you want to keep the direct coupling between them minimal if you can, but sometimes "reality" gets in the way. I've had some cases where I've passed in the View to the constructor of the ViewModel. Other cases where it's been an interface that the View implements and that gets passed into the ViewModel. So there are options. But you should make sure you HAVE to go that route before doing it.</p> <p>Example:</p> <p>XAML:</p> <pre><code>&lt;Window ...&gt; &lt;Window.Resources&gt; &lt;BooleanToVisibilityConverter x:Key="_B2VC" /&gt; &lt;/Window.Resources&gt; &lt;StackPanel&gt; &lt;Button Content="Save" Visibility="{Binding IsSaveButtonVisible}" /&gt; &lt;Button Content="Close" Visibility="{Binding IsCloseButtonVisible}" /&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>ViewModel:</p> <pre><code>public class ViewModel: INotifyPropertyChanged { #region INPC Stuff public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(String propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion private bool _IsSaveButtonVisible; public bool IsSaveButtonVisible { get { return _IsSaveButtonVisible; } set { if (_IsSaveButtonVisible != value) { _IsSaveButtonVisible = value; RaisePropertyChanged("IsSaveButtonVisible"); } } } private bool _IsCloseButtonVisible; public bool IsCloseButtonVisible { get { return _IsCloseButtonVisible; } set { if (_IsCloseButtonVisible != value) { _IsCloseButtonVisible = value; RaisePropertyChanged("IsCloseButtonVisible"); } } } } </code></pre> <p>Then your ViewModel changes those properties in response to whatever it needs to (say for instance Save is only valid if they've changed something - once that something is changed, the property on the ViewModel gets updated and bam, that gets propogated to the View.</p> <p>If you need further examples, i'd just suggest going and reading on MVVM. It takes a bit to grok, but its awesome once in use.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload