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

时间:2015-03-25 16:53:18

标签: c# wpf popup

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

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

    从其他地方搜索我知道我需要将Staysopen设置为false(它是) 我还读到了最好的方法是将IsOpen值绑定到视图模型中的属性并将其绑定设置为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>
    

    Codebehind在选择菜单项时在viewmodel上设置属性。

     private void CurrentPredicitions_OnClick(object sender, RadRoutedEventArgs e)
            {
    
                PredictionsPopup.Placement = PlacementMode.MousePoint;
                ViewModel.DisplaySummaryPopup = true;
    
            }
    

    Viewmodel属性

    public bool? DisplaySummaryPopup
            {
                get
                {
                    return this.displaySummaryPopup;
                }
    
                set
                {
                    this.displaySummaryPopup = value;
                    RaisePropertyChanged(() => this.DisplaySummaryPopup);
                }
            }
    

    如果您需要更多详细信息,请与我们联系。

4 个答案:

答案 0 :(得分:2)

这里面对一个有效的例子:

MainWindow 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>

MainWindow cs:

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

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

        }

视图模型:

  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实现是导致问题的实现。我自己尝试了代码,现在正在使用。

答案 1 :(得分:1)

在尝试跟踪此问题后,我发现该问题与上下文菜单有关。我知道这是因为根据上面的答案,而不是通过上下文菜单启动我的弹出窗口,我从测试按钮启动它,所有工作都按预期工作。

我仍然不知道这个问题的确切原因,但我认为它与上下文菜单本身是弹出窗口的子类这一事实有关,并且焦点未在新弹出窗口中正确设置,所以它永远不会检测到弹出丢失并关闭。

为了解决我的问题,我在弹出窗口中添加了一个关闭按钮,然后每当托管弹出窗口的控件中的活动选项卡发生更改时,它就会触发弹出窗口拾取和关闭的事件。

答案 2 :(得分:0)

有同样的问题。原因是,切换按钮的ClickMode属性设置为“ Press”。将其重新设置为“发布”即可解决:)。

答案 3 :(得分:0)

对我来说,解决方案是在弹出窗口代码隐藏的构造函数中添加这一行:

LostFocus += delegate { this.IsOpen = false; };

花了很多小时,当这样一条快速线路就完成了:)

相关问题