单击父UserControl时关闭当前窗口wpf [英] Close Current Window when parent UserControl is clicked wpf

查看:173
本文介绍了单击父UserControl时关闭当前窗口wpf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有一个用户控件按钮。点击该按钮将会启动一个窗口,如下所示:

Right now I have button on a usercontrol. Clicking that button will launch a window as follows,

 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;
        }


现在请参阅图片。每当我点击窗口小部件窗口外部,即任何时候我点击用户控件的灰色区域,我想要关闭圆形窗口小部件窗口。我试图为用户控件提出一个事件,如下所示,但事件不会触发。我也试过鼠标没有运气。我也试着将事件提升到用户控件的子容器,这是在我的情况下,但事件不会提高。请帮助

Now please see the Image. Whenever I click outside the widget window i.e. anytime I click on the grayed out area of the usercontrol, I want the circular widget window to close. I am trying to raise an event for the usercontrol as follows but the event never fires. I also tried mouse down with no luck. I also tried raising the events to the child container of the usercontrol which is Grid in my case but the event wont raise. Please help

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

这是WidgetWindow的代码。

Here is the code for the 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>


推荐答案

您可以使用Popup代替WidgetWindow。将所有的WidgetWindow内容放入弹出窗口。配置Popup Staysopen =False。您可以参考以下代码:

You can use Popup instead of WidgetWindow. Put all your WidgetWindow content into Popup. Configure Popup Staysopen="False". You can refer to below code:

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

使用popLink.IsOpen = true打开它。

Use popLink.IsOpen = true to open it.

这篇关于单击父UserControl时关闭当前窗口wpf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆