一个按钮上有多种样式和内容

时间:2017-10-16 09:33:39

标签: c# wpf xaml

我有一个HomePage,上面有许多按钮,我试图做几件事。首先,我在Style中有一个ResourceDictionary,它决定了按钮的整体“外观和感觉”。它看起来像这样:

<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>

没有什么太复杂,但我希望MouseOver效果在点击后保持不变。所以现在我正在做这样的事情:

<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>

我将绑定Button的command元素以设置ContactsClicked = true并在按下另一个按钮时重置它。但是,我收到的错误是内容已多次设置。

是否可以设置StyleButton,以及'点击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按钮',这是重复的代码

为了清楚起见,这就是我的目标:

enter image description here

2 个答案:

答案 0 :(得分:1)

我自己设法找到了解决方案,而不是使用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>

答案 1 :(得分:-1)

我会将此添加为评论,但我不允许。

如果您的目标是单击按钮后保持选中状态,为什么不使用StringRadioButton有一个名为RadioButton的内置属性,但也会保留&#34;已检查&#34;或&#34;选择&#34;即使多次点击后也是如此。

当然,您可以以类似的方式定义行为和样式,在属性IsChecked上创建一个简单的Trigger

如果需要更多解释,请说明。