[英]Multiple styles and content on one button
I have a HomePage
with a number of buttons on that I am trying to get to do a couple of things.我有一个HomePage
,上面有许多按钮,我正在尝试做一些事情。 Firstly, I have a Style
in a ResourceDictionary
that dictates the overall 'look and feel' of the buttons.首先,我在ResourceDictionary
中有一个Style
,它决定了按钮的整体“外观和感觉”。 It looks like this:它看起来像这样:
<Style TargetType="Button" x:Key="HomeButton">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="#06658D"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}" BorderBrush="White" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="#06658D"/>
</Trigger>
</Style.Triggers>
</Style>
Nothing too complicated, however I want the MouseOver effect to persist after being clicked.没什么太复杂的,但是我希望 MouseOver 效果在被点击后仍然存在。 So now I am looking at doing something like this:所以现在我正在考虑做这样的事情:
<Button Grid.Row="0" Style="{StaticResource HomeButton}" Content="Contacts">
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding ContactsClicked}" Value="True">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="#06658D"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button>
I'll bind the command element of the Button to set ContactsClicked = true
and reset this when another button is pressed.我将绑定按钮的命令元素以设置ContactsClicked = true
并在按下另一个按钮时重置它。 However, I get the error saying that Content is set multiple times.但是,我收到错误消息,指出内容已设置多次。
Is it possible to have an overall Style
of the Button
set, as well as a 'Clicked style and to have a
TextBlock displaying text on the button all at once or am I approaching this wrong? I am trying to not have an individual overall style for every single
是否可以设置Button
的整体Style
以及“单击style and to have a
TextBlock displaying text on the button all at once or am I approaching this wrong? I am trying to not have an individual overall style for every single
displaying text on the button all at once or am I approaching this wrong? I am trying to not have an individual overall style for every single
Button`as that's a lot of repeated code. displaying text on the button all at once or am I approaching this wrong? I am trying to not have an individual overall style for every single
Button displaying text on the button all at once or am I approaching this wrong? I am trying to not have an individual overall style for every single
因为这是很多重复的代码。
For clarity this is what I am aiming for:为清楚起见,这就是我的目标:
I've managed to find a solution myself, instead of using a Button
us a ToggleButton
instead:我自己设法找到了解决方案,而不是使用Button
我们使用ToggleButton
代替:
<Style TargetType="ToggleButton" x:Key="HomeButton">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="#06658D"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Background="{TemplateBinding Background}" BorderBrush="White" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Black" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="#06658D"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="#06658D"/>
</Trigger>
</Style.Triggers>
</Style>
I would add this as a comment but im not allowed.我会将此添加为评论,但我不允许。
If your goal is that the button will stay selected after it is clicked, why not use a RadioButton
?如果您的目标是按钮在点击后保持选中状态,为什么不使用RadioButton
? A RadioButton
has a built-in property called IsChecked
but will also remain "Checked" or "Selected" even after multiple clicks. RadioButton
有一个名为IsChecked
的内置属性,但即使在多次单击后也会保持“已选中”或“已选中”。
Of course you can define the behavior and the styles in a similar manner, create a simple Trigger
on the property IsChecked
.当然,您可以以类似的方式定义行为和样式,在属性IsChecked
上创建一个简单的Trigger
。
If more explanation is needed, just say so.如果需要更多解释,就直说。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.