使用按钮打开 WPF 弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24963938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open a WPF Popup with a button
提问by jarheadWill
I'm trying to open a Popup with a button and have implemented a basic ICommand. The button is binded to the ICommand OpenPopupCommand while the Popup IsOpen attribute is binded to the "IsOpen" OnPropertyChanged. My thought process was to bind the Popup.IsOpen attribute to the ICommand as well to have IT trigger the OnPropertyChange but couldn't get it to work. I think I'm close but can't figure it out. Here is the code I have so far:
我试图用一个按钮打开一个弹出窗口并实现了一个基本的 ICommand。该按钮绑定到 ICommand OpenPopupCommand,而 Popup IsOpen 属性绑定到“IsOpen”OnPropertyChanged。我的想法是将 Popup.IsOpen 属性绑定到 ICommand 以及让 IT 触发 OnPropertyChange 但无法使其工作。我想我很接近但无法弄清楚。这是我到目前为止的代码:
#region ICommand Members
private ICommand _openPopupCommand;
public ICommand OpenPopupCommand
{
get
{
if (_openPopupCommand == null)
_openPopupCommand = new RelayCommand(param => OpenPopupExecute(param));
return _openPopupCommand;
}
set
{
_openPopupCommand = value;
}
}
#endregion
#region Methods
public void OpenPopupExecute(object parameter)
{
parameter = true;
OnPropertyChanged("IsOpen");
}
#endregion
Button that "pops up" the Popup and the Popup XAML:
“弹出” Popup 和 Popup XAML 的按钮:
<Popup x:Name="FieldsPopup" Placement="Center" Width="400" Height="250" IsOpen="{Binding IsOpen}">
<StackPanel>
<TextBlock Background="LightBlue" HorizontalAlignment="Center" VerticalAlignment="Center" Height="250" Width="350" TextAlignment="Center" >This is a popup</TextBlock>
</StackPanel>
</Popup>
<Button Name="button_PatientIdentifierList" Width="23" Height="23" Grid.Column="2" Foreground="Black" Background="#FFCDCDCD" BorderBrush="#FF707070" Margin="3.4,4,4,0" VerticalAlignment="Top" Command="{Binding OpenPopupCommand}"/>
回答by Rachel
You're raising the PropertyChange notification, but I don't see you actually changing the property anywhere.
您正在提出 PropertyChange 通知,但我没有看到您实际上在任何地方更改了该属性。
Unless I'm mistaken, this code here takes the CommandParameter(called parameterhere) and sets it to true
除非我弄错了,这里的代码采用CommandParameter(parameter在此处调用)并将其设置为true
public void OpenPopupExecute(object parameter)
{
parameter = true;
OnPropertyChanged("IsOpen");
}
However in your XAML the Button.CommandParameter isn't bound to anything
但是,在您的 XAML 中,Button.CommandParameter 未绑定到任何内容
<Button Command="{Binding OpenPopupCommand}"/>
So I suspect that parameteris just null, and is not actually doing anything here.
所以我怀疑这parameter只是null,实际上并没有在这里做任何事情。
What you seem to be missing is the actual IsOpenproperty definition, and setting it to true in your command's Execute code :
您似乎缺少的是实际的IsOpen属性定义,并在您的命令的执行代码中将其设置为 true :
private bool _isOpen;
public bool IsOpen
{
get
{
return _isOpen;
}
set
{
_isOpen = value;
OnPropertyChanged("IsOpen");
}
}
public void OpenPopupExecute(object parameter)
{
IsOpen = true; // Will call OnPropertyChanged in setter
}
As a side note, I really don't like WPF's default PopupControl, and have a custom UserControl version of it on my blogif you ever decide you hate WPF's default PopupControltoo :)
附带说明一下,我真的不喜欢 WPF 的默认设置PopupControl,如果您决定也讨厌 WPF 的默认设置,我会在我的博客上提供它的自定义 UserControl 版本PopupControl:)

