使部分成为WPF窗口的点击对象,而不是其控件

时间:2018-08-13 12:59:42

标签: wpf user-interface background transparent click-through

为了实现阴影效果,我的窗口比其主要内容略大,并带有透明的“边框”。我很好奇是否有可能仅使该不可见的边框(图像中的彩色部分)点击并阻止主要内容被点击。

enter image description here

此线程说明了如何使整个窗口都点击通过: Making a WPF window click-through, but not its controls

有什么方法可以使这种方法适应我的预期目标?

1 个答案:

答案 0 :(得分:0)

只有具有完全透明背景的窗口才能真正单击。对于半透明窗口,您可以例如在单击阴影时自行最小化该窗口,例如:

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (e.OriginalSource == outer)
        WindowState = WindowState.Minimized;
}

XAML:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="300"
        AllowsTransparency="True" WindowStyle="None">
    <Window.Background>
        <SolidColorBrush Color="Red" Opacity="0.3" />
    </Window.Background>
    <Grid x:Name="outer" Background="Transparent" MouseLeftButtonDown="Grid_MouseLeftButtonDown">
        <Grid Background="Silver" Margin="10">
            <TextBlock>GUI</TextBlock>
        </Grid>
    </Grid>
</Window>