WPF Data Binding Architecture Considerations

I am trying to learn how to use WPF binding and MVVM architecture. I am having problems with Dependency Properties. I tried to control the visibility of the element in the view by binding it to DependencyProperty in the DataContext, but it does not work. No matter what I set the value GridVisiblein the constructor of the view model below, it is always displayed as visible when I run the code.

Can anyone see where I'm wrong?

C # code (ViewModel):

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.
    }
}

XAML Code (View):

<UserControl ... >

    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
    </UserControl.Resources>

    <UserControl.DataContext>
        <local:MyViewModel x:Name="myViewModel" />
    </UserControl.DataContext>

    <Grid x:Name="_myGrid"
        Visibility="{Binding Path=GridVisible,
            ElementName=myViewModel,
            Converter={StaticResource BoolToVisConverter}}">

        <!-- Other elements in here -->

    </Grid>

</UserControl>

I looked through a few tutorials online, and it seems to me that I am correctly following what I found there. Any ideas? Thank!

+5
3

ElementName , . :

<Grid x:Name="_myGrid"
        Visibility="{Binding Path=GridVisible,
            Converter={StaticResource BoolToVisConverter}}">
+2

, ViewModel INotifyPropertyChanged, DependencyObject. PropertyChanged .

    private bool gridVisible;

    public bool GridVisible
    {
        get { return gridVisible; }
        set 
        { 
            gridVisible = value; 
            OnPropertyChanged("GridVisible"); 
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
+1

ViewModel DataContext , , Path, DataContext , UserControl ( , , templated items of ItemsControl)

, DataContext UserControl, . ( ElementName, RelativeSource Source)

In addition, I personally would not inherit ViewModels from DependencyObject, as this introduces a thread-affinity, also the DependencyProperties point makes more sparse data structures more efficient without creating unnecessary fields in all of them (ViewModels are usually completely opposite to rare ones).

0
source

All Articles