0

我创建了一个UserControllike Popup,当用户单击菜单项时显示。

如果用户单击该用户控件应折叠的一侧。
当用户单击任何控件以外的一侧时,它对我来说很好。如果我单击数据网格或列表框,它不会隐藏。

这是我的代码:

<src:AddNewItemPopUp x:Name="PopUp" Margin="111,47,620,230"  Panel.ZIndex="1" Visibility="Collapsed"/>

我在点击事件中选择了一个按钮,我将PopUp可见性属性设置为 true

在我的用户控件中,我有网格。在网格的 mousedown 事件中,我编写了以下代码...

private void Grid_MouseDown_1(object sender, MouseButtonEventArgs e)
{
    if (PopUp.Visibility == Visibility.Visible)
    {
        PopUp.Visibility = Visibility.Collapsed;
    }
}

如果我单击放置在其中的任何控件(例如Button, DataGrid),则不会折叠。ListBoxGrid Popup

4

2 回答 2

0

尝试:

Visibility="Hidden" 

那是:

private void Grid_MouseDown_1(object sender, MouseButtonEventArgs e)
{
    if (PopUp.Visibility == Visibility.Visible)
    {
        PopUp.Visibility = Visibility.Hidden;
    }
}

还可以看到:

http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx

于 2012-12-28T12:53:31.580 回答
0

First Set Grid's Background Property, for example grid.Backgroung=Brushes.Transparent or in Xaml Backgroung = "Transparent"

Second Handle PreviewMouseDown event instead of MouseDown event.

The first one makes the mouse event to fire, when mouse is directly over the grid.

The second one makes the mouse event fire, when mouse is over an UIElement in the grid.

于 2012-12-28T14:42:34.583 回答