简体   繁体   English

堆栈面板中的wpf鼠标悬停边距

[英]wpf mouseover margin in stackpanel

I have a tab item, header having the following form: image_margin_textblock. 我有一个标签项,标题具有以下形式:image_margin_textblock。

The trigger IsMouseOver is working properly when the mouse cursor is over the image, and also over the textblock. 当鼠标光标在图像上方时,以及在文本块上方,触发器IsMouseOver正常工作。 But, when the mouse cursor is over the margin between the Image and Textblock, the IsMouseOver trigger is not fired. 但是,当鼠标光标位于Image和Textblock之间的边距上时,不会触发IsMouseOver触发器。 This creates an annoying flickering effect. 这会产生恼人的闪烁效果。

Do you have any ideas how to achieve mouseover trigger over the margin? 您是否有任何想法如何实现鼠标悬停触发?

Here is the code: 这是代码:

<TabItem.Header>
<ContentControl>
    <ContentControl.Template>
        <ControlTemplate>
            <StackPanel x:Name="sp0" Orientation="Horizontal">
                <StackPanel x:Name="sp1" Orientation="Horizontal" Background="Blue">
                    <Image VerticalAlignment="Center" HorizontalAlignment="Center" Source="tab1.png"/>
                </StackPanel>
                <TextBlock Margin="10,0,0,0" Text="Tab1" VerticalAlignment="Center"/>
            </StackPanel>
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}" Value="True">
                    <Setter TargetName="sp1" Property="StackPanel.Background" Value="Green"/>
                </DataTrigger>
                <Trigger SourceName="sp0" Property="IsMouseOver" Value="True">
                    <Setter TargetName="sp1" Property="StackPanel.Background" Value="Green"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ContentControl.Template>
</ContentControl>

Thank you. 谢谢。

Set Background on outer StackPanel to Transparent so that margin also participates in HitTest (ie respond to mouse events). 将外部StackPanel上的Background设置为Transparent以便margin也参与HitTest(即响应鼠标事件)。

Right now only image and TextBlock area responds to MouseOver event. 现在只有image和TextBlock区域响应MouseOver事件。 Setting background to Transparent will work. 将背景设置为透明将起作用。

<StackPanel x:Name="sp0" Orientation="Horizontal" Background="Transparent">

Set background of your StackPanel to Transparent . 将StackPanel的背景设置为Transparent This makes it visible to hit test. 这使得它可以进行测试。

    <StackPanel x:Name="sp0" Orientation="Horizontal" Background="Transparent">
            <StackPanel x:Name="sp1" Orientation="Horizontal" Background="Blue">
                <Image VerticalAlignment="Center" HorizontalAlignment="Center" Source="tab1.png"/>
            </StackPanel>
            <TextBlock Margin="10,0,0,0" Text="Tab1" VerticalAlignment="Center"/>
    </StackPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM