TabControl专注于弹出窗口

时间:2013-12-20 12:55:34

标签: c# wpf popup tabcontrol

我正在使用具有以下xaml的弹出控件

<Popup AllowsTransparency="True" IsOpen="{Binding PopupContext.IsOpen}"
       x:Name="__popup" StaysOpen="False">
  <TabControl Grid.Row="1">
    <TabItem Header="123"><Button>test</Button></TabItem>
  </TabControl>
</Popup>

如果我单击按钮并在弹出窗口外单击,弹出窗口将正常关闭。但是,如果我单击一个TabItem并单击父窗口外部(如果我单击桌面,弹出正常关闭),则不会关闭弹出窗口。

修改

我在ContextMenu.Opened事件中将Popup.IsOpen设置为true,因此UIElement是聚焦的,并且无法丢失焦点。如果触发了UIElement.ContextMenu.Closed事件并弹出了MouseLeaved,我将Popup.IsOpen属性设置为false。 (soo如果没有点击弹出窗口,它与ContextMenu关闭)

private void viewModelMasa_ContextMenuOpening(MasaViewModel Sender, System.Windows.Controls.ContextMenuEventArgs args)
    {
        MasaView source = args.Source as MasaView;
        ContextMenu menu = source.ContextMenu;
        RoutedEventHandler openHandler = null;
        RoutedEventHandler closeHandler = null;

        this.PopupContext.Position = System.Windows.Controls.Primitives.PlacementMode.Top;

        closeHandler = (sender, e) =>
            {
                if (!this.PopupContext.IsFocus)  // Setting from Popup MouseEnter and MouseLeave event;
                {
                    this.PopupContext.IsOpen = false;
                }
                menu.Closed -= closeHandler;
            };

        openHandler = (sender, e) =>
            {
                ContextMenu contextMenu = sender as ContextMenu;
                if (contextMenu != null)
                {

                    this.PopupContext.IsFocus = false;
                    System.Windows.Point point = new System.Windows.Point();
                    var pos = contextMenu.TranslatePoint(point,this.PopupContext.Parent);
                    this.PopupContext.Rectangle = new Rect(pos.X, pos.Y, contextMenu.ActualWidth, contextMenu.ActualHeight);
                    this.PopupContext.IsOpen = true;

                }
                menu.Opened -= openHandler;
            };
        menu.Opened += openHandler;
        menu.Closed += closeHandler;
    }

0 个答案:

没有答案