简体   繁体   English

在 wpf 中设置子元素的边距时,IsMouseOver 属性不起作用

[英]IsMouseOver property is not working when margin of child element is set in wpf

Recently, I am making a wpf application for chatting.最近在做一个wpf的聊天应用。 I want to change border brush color when mouseover on border.当鼠标悬停在边框上时,我想更改边框画笔颜色。 But, My code is not working because of margin or padding set.但是,由于设置了边距或填充,我的代码不起作用。

sample code follow as:示例代码如下:

MainWindow.xaml主窗口.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"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBlock Text="Test" Name="testBlock"/>

        <Border Name="test" BorderThickness="1" Width="300" Height="200">

            <TextBlock Text="Test1" Margin="30"/>


            <Border.Style>
                <Style TargetType="Border">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" Value="Red"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="False">
                            <Setter Property="BorderBrush" Value="Yellow"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
        </Border>
    </Grid>
</Window>

MainWindow.xaml.cs主窗口.xaml.cs

namespace WpfApp1
{
    public partial class MainWindow : Window
    {


        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

在此处输入图片说明

I want to change border brush when mouseover on border.我想在鼠标悬停在边框上时更改边框画笔。 How to make working well?如何让工作顺利?

A mouse over event works by detecting if the mouse is over a rendered pixel of a control.鼠标悬停事件的工作原理是检测鼠标是否在控件的渲染像素上。 This has the advantage, that if you make a round button with a border for example you will only be able to click it in the round section and not outside of it.这样做的好处是,例如,如果您制作一个带边框的圆形按钮,您将只能在圆形部分中单击它,而不能在圆形部分之外单击它。 This is because the color outside of the border is "null".这是因为边框外的颜色为“空”。 By actively setting it to transparent it will be rendered and thus be able to trigger the mouse over event.通过主动将其设置为透明,它将被渲染,从而能够触发鼠标悬停事件。

This will work:这将起作用:

        <Border Name="test" BorderThickness="1" Width="300" Height="200" Background="Transparent">

            <TextBlock Text="Test1" Margin="30"/>


            <Border.Style>
                <Style TargetType="Border">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" Value="Red"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="False">
                            <Setter Property="BorderBrush" Value="Yellow"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
        </Border>

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

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