0

I'm trying to bind the visibility flags of a pair of grids, however they appear to be ignoring my attempts. Any help would be appreciated. Thanks!

This is in my screens xaml:

<Grid Name="grdNotifLogo" Style="{StaticResource insightLogoNotify}" Visibility="{Binding Path=_notifVisibility, Mode=OneWay}" MouseDown="grdNotifLogo_MouseDown"/>
<Grid Name="grdMainLogo" Style="{StaticResource insightLogo}" Visibility="{Binding Path=_logoVisibility, Mode=OneWay}"/>

The screens xaml.cs file inherits a window base class, which has this:

protected Visibility _logoVisibility = _ Visibility.Visible;
protected Visibility _notifVisibility = _ Visibility.Collapsed;
public bool NotificationIconEnabled
{
    get { return _notifVisibility == Visibility.Visible; }
    set
    {
        if (value)
        {
            _logoVisibility = Visibility.Collapsed;
            _notifVisibility = Visibility.Visible;
        }
        else
        {
            _logoVisibility = Visibility.Visible;
            _notifVisibility = Visibility.Collapsed;
        }
    }
}

The data context is also setup to the grid which holds my grids of interest:

        ToolBar.DataContext = this;
4

1 回答 1

1

Yeah between Ryan, Adam and Tigran you have your answer. In code the full solution would look something like the sample below. Note that when you use the "Binding" keyword you are trying to access properties off the data context of your control. That is why you have to set the "DataContext" property of your control to a class that has those properties. As Ryan notes the properties must also be public, otherwise you won't be able to bind to them. Finally as Adam notes you have to use the INotifyPropertyChanged interface to communicate changes in the properties on your view-model object to your control.

So the XAML is tweaked to point at the new public property names:

<Grid Name="grdNotifLogo" Style="{StaticResource insightLogoNotify}" Visibility="{Binding NotificationVisibility}" MouseDown="grdNotifLogo_MouseDown"/>
<Grid Name="grdMainLogo" Style="{StaticResource insightLogo}" Visibility="{Binding LogoVisibility}"/>

Set the data context of your control:

class MyControl : UserControl
{
    public MyControl()
    {
        InitializeComponent();
        this.DataContext = new MyViewModel();
    }
}

Create a new view-model class to set as your data context:

class MyViewModel : INotifyPropertyChanged
{
    private Visibility _logoVisibility = Visibility.Visible;
    private Visibility _notifVisibility = Visibility.Collapsed;

    public Visibility LogoVisibility
    {
        get { return _logoVisibility; }
    }

    public Visibility NotificationVisibility
    {
        get { return _notifVisibility; }
    }

    public bool NotificationIconEnabled
    {
        get { return _notifVisibility == Visibility.Visible; }
        set
        {
            if (value)
            {
                _logoVisibility = Visibility.Collapsed;
                _notifVisibility = Visibility.Visible;
            }
            else
            {
                _logoVisibility = Visibility.Visible;
                _notifVisibility = Visibility.Collapsed;
            }
            OnPropertyChanged("LogoVisibility");
            OnPropertyChanged("NotificationVisibility");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

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

    #endregion // INotifyPropertyChanged Members
}
于 2012-06-26T23:48:05.530 回答