在 MouseOver WPF 上更改标签的前景色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13617064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change the Label's foreground colour on MouseOver WPF
提问by Guilherme Oliveira
I have the following problem: I have a Button, which has a StackPanelinside it, with a ContentControland a Labelwithin the StackPanel, and I've set the button's properties IsMouseOverand IsPressedto change the button's colour when the action happen. But, I would like to invert the label's colour when the mouse is over the button.
我有以下问题:我有一个Button,其中有一个StackPanel里面,有ContentControl和Label中StackPanel,和我设置按钮的属性IsMouseOver和IsPressed改变时的动作发生在按钮的颜色。但是,当鼠标悬停在按钮上时,我想反转标签的颜色。
This is my button's code: (because I think my description is not clear enough):
这是我按钮的代码:(因为我觉得我的描述不够清楚):
<Button Template="{StaticResource OnMouseOver}" ToolTip="Release" >
<StackPanel Orientation="Horizontal">
<ContentControl Template="{StaticResource Release}"/>
<Label Content="Release" Foreground="#457345" />
</StackPanel>
</Button>
This is how I changed the button's properties when the button is pressed and when the mouse is over the button:
这就是我在按下按钮和鼠标悬停在按钮上时更改按钮属性的方式:
<ControlTemplate TargetType="Button" x:Key="OnMouseOver">
<Border x:Name="border" Background="Transparent">
<ContentPresenter ContentSource="Content" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="Background" Value="#A1CCA1"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="border" Property="Background" Value="#AFD8AF"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
For the record, I've tried to do something like ControlTemplatefor my label, but it didn't work, and my label just vanished when I did.
为了记录,我试图ControlTemplate为我的标签做一些类似的事情,但没有成功,当我这样做时,我的标签消失了。
Could you help me?
你可以帮帮我吗?
回答by Hamza_L
try this :
尝试这个 :
<ControlTemplate TargetType="Button" x:Key="OnMouseOver">
<Border x:Name="border" Background="Transparent">
<StackPanel Orientation="Horizontal">
<ContentControl />
<Label x:Name="label" Content="Release" Foreground="#457345" />
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="Background" Value="#A1CCA1"/>
<Setter TargetName="label" Property="Foreground" Value="#AFD8AF"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="border" Property="Background" Value="#AFD8AF"/>
<Setter TargetName="label" Property="Foreground" Value="#A1CCA1"/>
</Trigger>
</ControlTemplate.Triggers>
<Button x:Name="button" Template="{StaticResource OnMouseOver}" ToolTip="Release" Height="42" VerticalAlignment="Bottom" Margin="235,0,127,143" >
</Button>

