祖先がマウスオーバーのときのボタンの視聴に関するWPF問題

StackOverflow https://stackoverflow.com/questions/4125555

  •  29-09-2019
  •  | 
  •  

質問

とりわけボタンを含むWPFグリッドがあります。

ボタンはデフォルトで隠されており、マウスがグリッドの上にあるときにのみ表示される必要があります。 (機能的には、グリッドはタブヘッダーであり、「消える」ボタンは閉じたボタンです)。また、ボタンテンプレートを書き直して、カスタムフィーリングをしました。

これで、マウスがグリッドに入るとボタンが表示されますが、マウスがボタンに入るとすぐに消えます。私の直感は、マウスがボタンに移動すると、グリッドのイスマウスオーバーが誤っていることです。これを回避する方法はありますか?

       <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