我正在开发一个应用程序,它需要一组togglebuttons来将过滤器应用于数据网格中显示的数据。按钮和togglebuttons的标准外观几乎没有启发,所以我一直在尝试为XAML中的togglebuttons构建一个样式。这就是我到目前为止拼凑的内容:
<Style x:Key="VerticalToggle" TargetType="{x:Type ToggleButton}">
<Setter Property="FontSize" Value="10" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="Width" Value="75" />
<Setter Property="Height" Value="52" />
<Setter Property="LayoutTransform">
<Setter.Value>
<RotateTransform Angle="90" />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border CornerRadius="0" Background="Gray" BorderBrush="Black" 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="IsChecked" Value="True">
<!-- Here, I have no idea... -->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
当我应用这种风格时,它可以按预期工作,但是当悬停或选择时,颜色不会改变。所以,我想......触发!但是,我不知道从哪里开始。我尝试过Setter Property =“background”Value =“White”作为Trigger的孩子但没有成功。
谢谢,
答案 0 :(得分:1)
要在“IsChecked”时更改背景,您可以使用:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Name="Border" Background="Gray" CornerRadius="0" BorderBrush="Black" 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="IsChecked" Value="True" >
<Setter Property="Background" Value="Black" TargetName="Border"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
我刚刚给边框命名并通过“TargetName”
在setter中引用它