我有一个WPF网格,其中包含一个按钮。

默认情况下,该按钮是隐藏的,只有在鼠标上方的网格上方时才能看到。 (从功能上讲,网格是一个标签标题,“消失”按钮是关闭按钮)。我还重写了按钮模板以具有自定义的感觉。

现在,当我的鼠标进入网格时,按钮变得可见,但是一旦鼠标进入按钮,就会消失。我的直觉是,当鼠标移动到按钮时,网格的iSmouseover变成了错误。有没有解决的办法?

       <ControlTemplate x:Key="CloseTabButtonTemplate">
            <Border Width="14" Height="14" Margin="3"
                    HorizontalAlignment="Right"
                    VerticalAlignment="Center"

                    BorderThickness="1"
                    CornerRadius="2,2,2,2">
                <TextBlock Text="x" VerticalAlignment="Center" HorizontalAlignment="Center"
                           FontSize="11" Padding="0" Margin="0,-2,0,0" Foreground="White"/>
                <Border.Style>

                    <Style TargetType="{x:Type Border}">
                        <Setter Property="Background" Value="#33DA3030"/>
                        <Setter Property="BorderBrush" Value="White"/>
                        <Setter Property="Visibility" Value="Hidden"/>
                        <Style.Triggers>                                
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Grid}},Path=IsMouseOver}" Value="True">
                                <Setter Property="Visibility" Value="Visible" />
                            </DataTrigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="#FFDA3030"/>
                                <Setter Property="Visibility" Value="Visible" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
            </Border>
        </ControlTemplate>          

<Button Grid.Column="2" HorizontalAlignment="Right" Template="{StaticResource CloseTabButtonTemplate}">x</Button>

谢谢!

有帮助吗?

解决方案

我在这里没有问题。我已经用一个带有网格的简单窗口对其进行了测试:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"
        >

    <Window.Resources>

        <ControlTemplate x:Key="CloseTabButtonTemplate">
            .... Your template as in the question ....
        </ControlTemplate>

    </Window.Resources>

    <Grid x:Name="MainGrid" Width="200" Height="200" Background="Transparent">

        <Button Template="{StaticResource CloseTabButtonTemplate}" />

    </Grid>
</Window>

也许父母网格缺少 Background 颜色?如您所见,我做了我的 Grid Transparent. 。当我删除它时,它不会显示按钮。

也许添加更多代码?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top