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

问题描述 投票:0回答:1

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

 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

看图片。每当我在小部件窗口外部单击时,即每当我单击用户控件的灰色区域时,我都希望关闭圆形小部件窗口。我试图为用户控件引发一个事件,如下所示,但该事件从未触发。

我也尝试过按下鼠标,但没有成功。我还尝试将事件引发到用户控件的子容器,在我的例子中是 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>
c# wpf c++-cli
1个回答
1
投票

您可以使用 Popup 代替 WidgetWindow。将所有 WidgetWindow 内容放入 Popup 中。配置弹出窗口保持打开=“False”。您可以参考以下代码:

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

使用 popLink.IsOpen = true 打开它。

© www.soinside.com 2019 - 2024. All rights reserved.