如果我设置数据上下文,可见性 属性 不起作用,反之亦然

Visibility property not working if i set datacontext and vice versa

我希望用户控件在选择菜单时可见。

只要用户单击视觉菜单,就会显示 AV_Credentials 用户控件。但是我无法为新用户控件做数据上下文。

Menu.xaml

<UserControl x:Class="Connector.Views.Menu"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Connector.Views"
         xmlns:menuViewModel="clr-namespace:Connector.ViewModel.Menu"
         xmlns:ViewModel="clr-namespace:Connector.ViewModel.AV_Credentials"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="350">
<UserControl.DataContext>
    <menuViewModel:Menu_ViewModel/>
</UserControl.DataContext>
<Grid>
    <StackPanel>
        <Menu HorizontalAlignment="Stretch" VerticalAlignment="Top" >
            <MenuItem Header="Credentials">
                <MenuItem Header="vision"  Command="{Binding Vision}"/>
                <MenuItem Header="NOP" Command="{Binding NOP}"/>
            </MenuItem>
            <MenuItem Header="Sync"/>
        </Menu>
        <local:AV_Credentials Visibility="{Binding Path=AVCred}" DataContext="{Binding AV_Context}"/>
    </StackPanel>
</Grid>

Menu_ViewModel.cs

class Menu_ViewModel : INotifyPropertyChanged
{
    private AV_Credentials_ViewModel _av_Context;
    public AV_Credentials_ViewModel AV_Context
    {
        get
        {
            if(_av_Context == null)
            {
                _av_Context = new AV_Credentials_ViewModel();
            }
            return _av_Context;
        }
    }

    private Visibility _cred = Visibility.Hidden;
    public Visibility Cred
    {
        get
        {
            return _cred;
        }
        set
        {
            _cred = value; OnPropertyChanged("Cred");
        }
    }

    private ICommand mUpdater;
    public ICommand vision
    {
        get
        {
            if (mUpdater == null)
                mUpdater = new Updater(this);
            return mUpdater;
        }
        set
        {
            mUpdater = value;
        }
    }

    private class Updater : ICommand
    {
        private Menu_ViewModel obj;
        public Updater(Menu_ViewModel _obj)
        {
            obj = _obj;
        }
        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            obj.ShowAVCred();
        }

        #endregion
    }

    public void ShowAVCred()
    {
        Cred = Visibility.Visible;
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

我想要实现的是 -

  1. 菜单将为空白。
  2. 当用户从菜单中选择视觉时,用户控件将显示在菜单中。
  3. 用户会提供一些数据。

怎么了

可见性部分工作正常,但用户控件中的按钮和文本框不工作。如果我分配数据上下文,按钮和文本框可以工作,但可见性不工作。

如果需要,我可以分享 AV_Credential .xaml 和 .cs

抱歉英语不好

If i assign datacontext, the button and textboxes are working, but the visibility is not working.

将"AV_Context."添加到AV_Credentials.xaml中的绑定路径,例如:

<TextText Text="{Binding AV_Context.YourProperty}" />

这应该可以工作,因为 AV_Credentials 控件的 DataContextMenu_ViewModel 并且此类型有 AV_Context 属性。

您不应显式设置 UserControlDataContext,因为那样绑定到 Menu_ViewModelCred 属性 将会失败已经发现了

另一种选择是为 Visibility 绑定指定一个显式来源:

<local:AV_Credentials Visibility="{Binding Path=DataContext.Cred, RelativeSource={RelativeSource AncestorType=UserControl}}" 
                      DataContext="{Binding AV_Context}"/>