单击父UserControl时关闭当前窗口wpf

时间:2016-05-18 00:18:26

标签: c# wpf c++-cli

现在我在usercontrol上有按钮。单击该按钮将启动一个窗口,如下所示,

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Effect = new BlurEffect();
            dlg = new WidgetWindow();
            dlg.Owner = Window.GetWindow(this);
            dlg.Show();
            this.Effect = null;
        }

enter image description here 现在请看图像。每当我在窗口小部件窗口外单击时,即在我单击用户控件的灰色区域时,我希望关闭圆形窗口小部件窗口。我正在尝试为usercontrol引发一个事件,如下所示,但事件永远不会触发。我也尝试过鼠标而没有运气。我也尝试将事件提升到usercontrol的子容器,在我的情况下是Grid,但事件不会引发。请帮忙

 private void Tbl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (dlg != null)
            {
                dlg.Close();
            }
        }

以下是WidgetWindow的代码。

<Window WindowStyle="None" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowState="Maximized"
        AllowsTransparency="True" Background="#80000000"
        Title="WidgetWindow" ShowInTaskbar="False" Loaded="widget_Loaded">
    <Grid>
            <Grid.Background>
                <SolidColorBrush Opacity="0.5" Color="White"></SolidColorBrush>
            </Grid.Background>

            <ListBox BorderThickness="0" Name="lstBox" Height="980" Width="980" ItemsSource="{Binding}">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <local:CircularPanel />
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.Background>
                    <SolidColorBrush Opacity="0" Color="Gray"></SolidColorBrush>
                </ListBox.Background>

            </ListBox>

            <Grid Height="524" Width="524" Margin="54,36,0,0">
                <Ellipse Stroke="Black" StrokeThickness="1">
                    <Ellipse.Fill>
                        <RadialGradientBrush>
                            <GradientStop Color="White" Offset="0"></GradientStop>
                        </RadialGradientBrush>
                    </Ellipse.Fill>
                </Ellipse>

                <Grid Name="Inner1" Margin="0,5,0,0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Button Grid.Row="0" Name="btn1" Width="130" Height="130" HorizontalAlignment="Center" VerticalAlignment="Top" Click="btn1_Click">
                        <Button.Template>
                            <ControlTemplate TargetType="Button">


                                <Ellipse Grid.Row="0" Stroke="Black" StrokeThickness="1">
                                    <Ellipse.Fill>
                                        <ImageBrush ImageSource="Images/fruits.jpg" Stretch="Fill"></ImageBrush>
                                    </Ellipse.Fill>
                                </Ellipse>



                            </ControlTemplate>

                        </Button.Template>
                    </Button>
                    <TextBlock Margin="0,4,0,0" FontSize="12" HorizontalAlignment="Center" Grid.Row="1">Locate Sessions</TextBlock>

                </Grid>

                <Grid Name="Inner2" Margin="34,124,0,150">

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Button Grid.Row="0"  Name="btn2" Width="130" Height="130" HorizontalAlignment="Left" VerticalAlignment="Center" Click="btn2_Click">

                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Ellipse  Stroke="Black" StrokeThickness="1">
                                    <Ellipse.Fill>
                                        <ImageBrush ImageSource="Images/bird.jpg" Stretch="Fill"></ImageBrush>
                                    </Ellipse.Fill>
                                </Ellipse>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                    <TextBlock Margin="20,4,0,0" FontSize="12" HorizontalAlignment="Left" Grid.Row="1">Mass Surveillence</TextBlock>
                </Grid>

                <Grid Name="Inner3" Margin="0,124,34,150">

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Button Grid.Row="0" Name="btn3" Width="130" Height="130" HorizontalAlignment="Right" VerticalAlignment="Center" Click="btn3_Click">
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Ellipse Stroke="Black" StrokeThickness="1">
                                    <Ellipse.Fill>
                                        <ImageBrush ImageSource="Images/forest.jpg" Stretch="Fill"></ImageBrush>
                                    </Ellipse.Fill>
                                </Ellipse>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>

                    <TextBlock Margin="0,4,8,0" FontSize="12" HorizontalAlignment="Right" Grid.Row="1">Subscriber Intelligence</TextBlock>
                </Grid>

                <Grid Name="Inner4" Margin="34,285,0,0">

                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Button Grid.Row="0" Name="btn4" Width="130" Height="130" HorizontalAlignment="Left" VerticalAlignment="Center" Click="btn4_Click">
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Ellipse Stroke="Black" StrokeThickness="1">
                                    <Ellipse.Fill>
                                        <ImageBrush ImageSource="Images/nature.jpg" Stretch="Fill"></ImageBrush>
                                    </Ellipse.Fill>
                                </Ellipse>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                    <TextBlock Margin="45,4,0,0" FontSize="12" HorizontalAlignment="Left" Grid.Row="1">Analytics</TextBlock>
                </Grid>

                <Grid Name="Inner5" Margin="0,285,34,0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                    </Grid.RowDefinitions>
                    <Button Grid.Row="0" Name="btn5" Width="130" Height="130" HorizontalAlignment="Right" VerticalAlignment="Center" Click="btn5_Click">
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Ellipse Stroke="Black" StrokeThickness="1">
                                    <Ellipse.Fill>
                                        <ImageBrush ImageSource="Images/beach.jpg" Stretch="Fill"></ImageBrush>
                                    </Ellipse.Fill>
                                </Ellipse>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                    <TextBlock Margin="0,4,40,0" FontSize="12" HorizontalAlignment="Right" Grid.Row="1">Historical</TextBlock>
                </Grid>

                <Grid Name="Inner6" Margin="0,0,0,10" >
                    <Button Name="btn6" Width="130" Height="130" HorizontalAlignment="Center" VerticalAlignment="Bottom" Click="btn6_Click">
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Ellipse Stroke="Black" StrokeThickness="1">
                                    <Ellipse.Fill>
                                        <ImageBrush ImageSource="Images/Exit.jpg" Stretch="Fill"></ImageBrush>
                                    </Ellipse.Fill>
                                </Ellipse>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                </Grid>

                <Grid Name="Inner7" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <Button Name="btn7" Width="130" Height="130">
                        <Button.Template>
                            <ControlTemplate TargetType="Button">
                                <Ellipse Name="pointerEllipse" Stroke="Black" StrokeThickness="1">

                                </Ellipse>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                </Grid>
            </Grid>
            <!--</ControlTemplate>
                    </Button.Template>

                </Button>-->
        </Grid>
</Window>

1 个答案:

答案 0 :(得分:1)

您可以使用Popup而不是WidgetWindow。将所有WidgetWindow内容放入Popup。配置Popup Staysopen =“False”。您可以参考以下代码:

<Popup Name="popLink" StaysOpen="False" Placement="Mouse" MaxWidth="200"
PopupAnimation="Slide" AllowsTransparency="True">
...Your content
</Popup>

使用popLink.IsOpen = true打开它。