Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Data Binding Architecture Question
    primarykey
    data
    text
    <p>I'm trying to learn how to use WPF binding and the MVVM architecture. I'm running into some trouble with Dependency Properties. I've tried to control the visibility of an item on the view by binding it to a DependencyProperty in the DataContext, but it doesn't work. No matter what I set the <code>GridVisible</code> value to in the constructor of the view model below, it is always displayed as visible when I run the code.</p> <p>Can anyone see where I'm going wrong?</p> <p><strong>C# code</strong> (ViewModel):</p> <pre><code>public class MyViewModel : DependencyObject { public MyViewModel () { GridVisible = false; } public static readonly DependencyProperty GridVisibleProperty = DependencyProperty.Register( "GridVisible", typeof(bool), typeof(MyViewModel), new PropertyMetadata(false, new PropertyChangedCallback(GridVisibleChangedCallback))); public bool GridVisible { get { return (bool)GetValue(GridVisibleProperty); } set { SetValue(GridVisibleProperty, value); } } protected static void GridVisibleChangedCallback( DependencyObject source, DependencyPropertyChangedEventArgs e) { // Do other stuff in response to the data change. } } </code></pre> <p><strong>XAML code</strong> (View):</p> <pre><code>&lt;UserControl ... &gt; &lt;UserControl.Resources&gt; &lt;BooleanToVisibilityConverter x:Key="BoolToVisConverter" /&gt; &lt;/UserControl.Resources&gt; &lt;UserControl.DataContext&gt; &lt;local:MyViewModel x:Name="myViewModel" /&gt; &lt;/UserControl.DataContext&gt; &lt;Grid x:Name="_myGrid" Visibility="{Binding Path=GridVisible, ElementName=myViewModel, Converter={StaticResource BoolToVisConverter}}"&gt; &lt;!-- Other elements in here --&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>I've looked at several tutorials online, and it seems like I'm correctly following what I've found there. Any ideas? Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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