从主模块绑定用户控件的控件绑定



我有WPF MVVM应用程序。我有一个弹出式的用户控件。当我点击用户控件的一个按钮(父绑定)时,我希望显示弹出窗口。(同样关闭)

Command="{Binding Parent.ShowPopupCommand}"              
<Popup Name="Popup1" IsEnabled="True"              
IsOpen="{Binding DisplayHelper.IsOpenPopup, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
 </Popup>

我没有在用户控件中编写依赖属性,而是使用INotifyPropertyChanged接口编写了单独的视图模型。登录时,我正在从login.cs 绑定弹出IsOpen属性

RelayCommand _showPopupCommand;
RelayCommand _hidePopupCommand;

public ICommand ShowPopupCommand
        {
            get
            {
                if (_showPopupCommand == null)
                {
                    _showPopupCommand = new RelayCommand(param => this.ShowPopup(), null);
                }
                return _showPopupCommand;
            }
        }
        public ICommand HidePopupCommand
        {
            get
            {
                if (_hidePopupCommand == null)
                {
                    _hidePopupCommand = new RelayCommand(param => this.HidePopup(), null);
                }
                return _hidePopupCommand;
            }
        }
        private void HidePopup()
        {
            DisplayHelper ds = new DisplayHelper();
            ds.IsOpenPopup = false;
        }
        private void ShowPopup()
        {
            DisplayHelper ds = new  DisplayHelper();
            ds.IsOpenPopup = true;
        }

但点击时不会显示弹出窗口。

请在此中提供帮助

您的问题是每次运行命令时都会创建DisplayHelper的新实例,但View会在ViewModel中查找DisplayHelper属性。

为了解决这个问题,我建议您将DisplayHelper设置为ViewModel中的一个属性。

我希望它能有所帮助,如果你需要我详细说明,请随时询问。快乐编码。:)

最新更新