弹出窗口未关闭

时间:2014-07-14 08:21:34

标签: c# .net wpf popup

通过快捷方式导航到其他用户控件后,仍会打开弹出窗口。 Staysopen道具是假的

FocusManager.SetFocusedElement没有帮助。

只找到一个丑陋,不好的答案 - 模拟点击winapi,但我不想使用它。 目前我正在做这样的事情

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

    private const int MOUSEEVENTF_LEFTDOWN = 0x02;
    private const int MOUSEEVENTF_LEFTUP = 0x04;
    private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
    private const int MOUSEEVENTF_RIGHTUP = 0x10;

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool GetCursorPos(ref Win32Point pt);

    [StructLayout(LayoutKind.Sequential)]
    internal struct Win32Point
    {
        public Int32 X;
        public Int32 Y;
    };
    private static Point GetMousePosition()
    {
        Win32Point w32Mouse = new Win32Point();
        GetCursorPos(ref w32Mouse);
        return new Point(w32Mouse.X, w32Mouse.Y);
    }

    private void ShowOpenControlExecute(object sender, ExecutedRoutedEventArgs e)
    {
        var labelEditor = WorkspaceService.SelectedItem.Content as LabelEditor;
        var mousepoint = GetMousePosition();

        Menu.OpenedTabName = WorkspaceService.SelectedItem.Name;
        Menu.Visibility = Visibility.Visible;
        Menu.ShowOpenControl();
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, Convert.ToUInt32(mousepoint.X), Convert.ToUInt32(mousepoint.Y), 0, 0);
    }

2 个答案:

答案 0 :(得分:0)

您应该使用Popup.IsOpen property打开和关闭Popup控件。这是一个非常简单的XAML示例,演示了这一点:

<Grid>
    <ToggleButton Name="ToggleButton" Content="Click to toggle Popup" />
    <Popup Placement="Relative">
        <Border Background="White" CornerRadius="3" Padding="3">
            <TextBlock Text="I'm a Popup" />
        </Border>
        <Popup.Style>
            <Style TargetType="{x:Type Popup}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, 
                        ElementName=ToggleButton}" Value="True">
                        <Setter Property="IsOpen" Value="True" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Popup.Style>
    </Popup>
</Grid>

答案 1 :(得分:0)

我们决定在打开弹出窗口时限制使用快捷方式(下拉列表等)。

这是我们唯一的解决方案