WPF绑定到继承的值

时间:2012-06-26 15:49:37

标签: c# wpf xaml

我正在尝试绑定一对网格的可见性标记,但是它们似乎忽略了我的尝试。任何帮助,将不胜感激。谢谢!

这是我的屏幕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}"/>

屏幕xaml.cs文件继承了一个窗口基类,其中包含:

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

数据上下文也设置在网格中,该网格包含我感兴趣的网格:

        ToolBar.DataContext = this;

1 个答案:

答案 0 :(得分:1)

在Ryan,Adam和Tigran之间,你有答案。在代码中,完整的解决方案看起来像下面的示例。请注意,当您使用“绑定”关键字时,您尝试访问控件的数据上下文中的属性。这就是为什么你必须将控件的“DataContext”属性设置为具有这些属性的类。正如Ryan所说,这些属性也必须是公开的,否则你将无法绑定它们。最后,正如Adam所说,您必须使用INotifyPropertyChanged接口将视图模型对象的属性更改传达给您的控件。

因此,调整XAML以指向新的公共属性名称:

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

设置控件的数据上下文:

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

创建一个新的视图模型类以设置为您的数据上下文:

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
}