我是WPF的新手。我想更改MouseEnter
上的按钮文字和颜色。
这是我的代码,以便改变颜色:
<Style x:Key="btnClose" TargetType="{x:Type Button}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button" x:Name="btnClose">
<Border Name="btnCloseBorder" CornerRadius="7" Background="Red" BorderBrush="#ccc" BorderThickness="0,1,1,0" >
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Button.Content">
<Setter.Value>
<ContentElement ???/>
</Setter.Value>
</Setter>
</Trigger>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<BeginStoryboard>
<Storyboard>
<ColorAnimation From="Red" To="#FF7F7F" Duration="0:0:0.5" Storyboard.TargetName="btnCloseBorder" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation From="#FF7F7F" To="Red" Duration="0:0:0.5" Storyboard.TargetName="btnCloseBorder" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Button Style="{StaticResource btnClose}" Name="btnClose" Content="" HorizontalAlignment="Left" Margin="274,0,0,0" VerticalAlignment="Top" Width="18" Height="21"/>
它就像一个魅力。但我无法改变文字。
我邀请它应该在ColorAnimation
标记之后。但我不知道应该使用什么标签。
如何更改MouseEnter
和MouseLeave
中的按钮文字?
答案 0 :(得分:1)
我找到了答案。我只是删除了Content
代码中的Button
并更改了Style
,如下所示:
<Style x:Key="btnClose" TargetType="{x:Type Button}" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button" x:Name="btnClose">
<Border Name="btnCloseBorder" CornerRadius="7" Background="Red" BorderBrush="#ccc" BorderThickness="0,1,1,0" >
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Button.Content" Value="Your Text">
</Setter>
</Trigger>
<EventTrigger RoutedEvent="Mouse.MouseEnter">
<BeginStoryboard>
<Storyboard >
<ColorAnimation From="Red" To="#FF7F7F" Duration="0:0:0" Storyboard.TargetName="btnCloseBorder" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave">
<BeginStoryboard>
<Storyboard>
<ColorAnimation From="#FF7F7F" To="Red" Duration="0:0:0.5" Storyboard.TargetName="btnCloseBorder" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property = "Foreground" Value="White"/>
<Setter Property = "Button.Content" Value="Some new text..."/>
</Trigger>
</Style.Triggers>
</Style>
<Button Style="{StaticResource btnClose}" Name="btnClose" HorizontalAlignment="Left" Margin="274,0,0,0" VerticalAlignment="Top" Width="18" Height="21"/>
我希望它会有用。
答案 1 :(得分:0)
您不需要显式创建ContentControl。您可以将Button.Content
设置为您想要的任何对象。如果对象不是UIElement
,则会创建隐式TextBlock
,并将其文本设置为Value.ToString()
。如果是UIElement
,则会使用OnRender
方法对其进行渲染。
因此,在您的情况下,您只需将setter值设置为字符串。
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Button.Content" Value="Your Text">
</Setter>
</Trigger>
请注意,当IsMouseOver
变为false时,此触发器将自动恢复为之前的值。