[英]WPF Usercontrol XAML Triggers not working
I am making an UserControl with XAML which is a simple border with a Dock Panel inside it. 我用XAML制作了一个UserControl,它是一个带有Dock Panel的简单边框。 And inside the Dock there is an image and text bellow as properties ( i got that all figured out already). 并且在Dock内部有一个图像和文本波纹管作为属性(我已经全部弄清楚了)。
However i need the Border that contains everything to change color on the MouseEnter event of the DockPanel and change it again to default when the MouseLeave event is triggered. 但是,我需要包含所有内容的Border来更改DockPanel的MouseEnter事件的颜色,并在触发MouseLeave事件时将其再次更改为默认值。
It compiles just fine. 它编译就好了。 The problem is that when i use it on a normal window the Triggers are not even working and the mouse cursor doesnt change when i hover the DockPanel. 问题是,当我在普通窗口上使用它时,触发器甚至都无法工作,并且当我将鼠标悬停在DockPanel上时,鼠标光标也不会改变。
<UserControl x:Name="StartBtn" x:Class="NMis.Controls.StartCenterButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="155" d:DesignWidth="155" FontFamily="Open Sans" TextOptions.TextFormattingMode="Display" ScrollViewer.VerticalScrollBarVisibility="Disabled" UseLayoutRounding="True" IsHitTestVisible="False">
<Border BorderThickness="1.5" Height="155" Width="155" CornerRadius="2">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop x:Name="Grad1" Color="#FF151515" Offset="0"/>
<GradientStop x:Name="Grad2" Color="#E54D9EA9" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
<DockPanel x:Name="Condominio_Dock" Margin="5,10,5,5" Cursor="Hand" Background="#FFF" MouseDown="DockPanel_MouseDown">
<DockPanel.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter" SourceName="Condominio_Dock">
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="#5fb357" Storyboard.TargetName="Grad1" Duration="0:0:0.5" Storyboard.TargetProperty="Color" From="#FF151515"></ColorAnimation>
<ColorAnimation To="#FF151515" Storyboard.TargetName="Grad2" Duration="0:0:0.5" Storyboard.TargetProperty="Color" From="#E54D9EA9"></ColorAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave" SourceName="Condominio_Dock">
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="#FF151515" Storyboard.TargetName="Grad1" Duration="0:0:0.5" Storyboard.TargetProperty="Color" From="#5fb357"></ColorAnimation>
<ColorAnimation To="#E54D9EA9" Storyboard.TargetName="Grad2" Duration="0:0:0.5" Storyboard.TargetProperty="Color" From="#FF151515"></ColorAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</DockPanel.Triggers>
<Image UseLayoutRounding="True" Stretch="Uniform" Source="{Binding ElementName=StartBtn, Path=Image}" Height="104" DockPanel.Dock="Top"></Image>
<TextBlock Text="{Binding ElementName=StartBtn, Path=Text}" DockPanel.Dock="Top" TextTrimming="WordEllipsis" Margin="0,5,0,0" TextAlignment="Center" FontWeight="SemiBold"></TextBlock>
</DockPanel>
</Border>
</UserControl>
You can also get this problem is the dock panel doesn't have a brush. 您还可能会遇到此问题,因为扩展坞面板没有刷子。 Because then its transparent it doesn't get found by the hit test. 因为那样的话,它的透明性就不会在点击测试中找到。
Fixed it. 修复。 Apparently IsHitTestVisible must be true for the DockPanel use Triggers 显然,DockPanel使用触发器的IsHitTestVisible必须为true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.