c# - 即使使用 StaysOpen ="false",WPF 弹出窗口也不会关闭

标签 c# wpf popup

我正在努力实现以下目标:

  1. 用户在数据网格中调出上下文菜单。
  2. 用户选择一个上下文菜单项,然后打开一个弹出窗口并显示一些信息。
  3. 当用户单击应用程序中的其他任何地方而不是弹出窗口时,弹出窗口将关闭。

在我关闭弹出窗口之前一切正常。

从其他地方搜索我知道我需要将 Staysopen 设置为 false(它是) 我还读到最好的方法是将 IsOpen 值绑定(bind)到 View 模型中的属性并将其绑定(bind)设置为 2 方式(也已完成)

作为旁注,我发现如果我添加一个文本框并在框内单击,然后当我在弹出窗口外单击时,它会根据需要关闭。

作为解决方法,我尝试过的另一件事没有成功,是以编程方式将键盘焦点设置在文本框上以获得我想要的“自动关闭”功能。

代码如下:

xaml-

<Popup Name="PredictionsPopup" Height="200" Width="200" AllowsTransparency="false" StaysOpen="False" IsOpen="{Binding DisplaySummaryPopup, Mode=TwoWay}">
            <StackPanel Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
                <TextBlock Text="here is some stuff" />
                <TextBox Name="hiddenBox" Text="moo"/>
            </StackPanel>         
        </Popup>

选择菜单项时设置 View 模型属性的代码隐藏。

 private void CurrentPredicitions_OnClick(object sender, RadRoutedEventArgs e)
        {

            PredictionsPopup.Placement = PlacementMode.MousePoint;
            ViewModel.DisplaySummaryPopup = true;

        }

View 模型属性

public bool? DisplaySummaryPopup
        {
            get
            {
                return this.displaySummaryPopup;
            }

            set
            {
                this.displaySummaryPopup = value;
                RaisePropertyChanged(() => this.DisplaySummaryPopup);
            }
        }

如果您需要更多详细信息,请告诉我。

最佳答案

这里有一个工作示例:

主窗口 XAML:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Popup Name="PredictionsPopup" Height="200" Width="200" AllowsTransparency="false" StaysOpen="False" IsOpen="{Binding DisplaySummaryPopup, Mode=TwoWay}">
            <StackPanel Background="Red">
                <TextBlock Text="here is some stuff" />
                <TextBox Name="hiddenBox" Text="moo"/>
            </StackPanel>
        </Popup>
        <DataGrid AutoGenerateColumns="False"  Name="dataGrid1"  IsReadOnly="True" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Site"  Width="150" />
                <DataGridTextColumn Header="Subject"  Width="310" />
            </DataGrid.Columns>
            <DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Click Me" Click="ButtonBase_OnClick">                     
                    </MenuItem>
                </ContextMenu>
            </DataGrid.ContextMenu>
        </DataGrid>
      

    </Grid>
</Window>

主窗口 :

 public MainWindow()
        {
            InitializeComponent();
            DataContext = new TestViewModel();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            PredictionsPopup.Placement = PlacementMode.MousePoint;
            PredictionsPopup.IsOpen = true;

        }

View 模型:

  public class TestViewModel : INotifyPropertyChanged
    {
        private bool _displaySumarry;
        public bool DisplaySummaryPopup
        {
            get { return _displaySumarry;  }
            set
            {
                _displaySumarry = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

我认为您对 INotifyPropertyChanged 的​​实现是导致问题的原因。我自己尝试了代码并且现在正在工作。

关于c# - 即使使用 StaysOpen ="false",WPF 弹出窗口也不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29261764/

相关文章:

c# - 在 Entity Framework 中加入自定义类型

c# - 模态对话框导致 UI 自动化挂起

jquery - 设置 jQuery cookie 仅显示一次弹出窗口

javascript - 仅当这些消息存在时,如何在 primefaces 中显示带有 requiredMessages 的弹出窗口?

javascript - 使用 OpenLayers,删除标记层和弹出窗口的正确方法是什么?

c# - 如何关闭所有 Selenium chromedriver/IEdriver 打开的窗口?

c# - 通过 IP 阻止连接

c# - 如何编写一个方法来接受一个类型作为参数,并在方法体内使用它来将另一个变量转换为该传递的类型?

c# - ObservableCollection 引发 "Item[]"的 PropertyChange 的目的是什么?

wpf - MVVM 模式中的失去焦点事件策略