使WPF窗口的一部分点击,而不是它的控件

问题描述 投票:-2回答:1

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

enter image description here

该主题解释了如何使整个窗口点击:Making a WPF window click-through, but not its controls

有没有办法让这种方法适应我打算实现的目标?

wpf user-interface background transparent click-through
1个回答
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> 
© www.soinside.com 2019 - 2024. All rights reserved.