C# WPF切换按钮模板边框笔刷
**编辑-我将DynamicSource答案标记为该问题的答案。这解决了我在这里描述的问题。我在我的主应用程序中仍然遇到问题,结果证明这是因为我在其他地方使用画笔作为静态资源,然后在我的边界上使用它作为动态资源。当我把一切都切换到DynamicSource时,它工作正常。谢谢 我似乎无法在模板化的切换按钮中使用BorderBrush。这是我的示例.xaml,如果您运行它,您将看到当您将鼠标悬停或选中其中一个按钮时,边框变得透明C# WPF切换按钮模板边框笔刷,c#,wpf,togglebutton,C#,Wpf,Togglebutton,**编辑-我将DynamicSource答案标记为该问题的答案。这解决了我在这里描述的问题。我在我的主应用程序中仍然遇到问题,结果证明这是因为我在其他地方使用画笔作为静态资源,然后在我的边界上使用它作为动态资源。当我把一切都切换到DynamicSource时,它工作正常。谢谢 我似乎无法在模板化的切换按钮中使用BorderBrush。这是我的示例.xaml,如果您运行它,您将看到当您将鼠标悬停或选中其中一个按钮时,边框变得透明 <Window x:Class="ToggleButtonSt
<Window x:Class="ToggleButtonStyle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ToggleButtonStyle"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!-- <ResourceDictionary Source="Dictionary1.xaml" /> -->
<!-- main color of buttons-->
<Color x:Key="MainThemeColor">Orange</Color>
<!-- hover-over color for buttons -->
<Color x:Key="MouseOverColor">Purple</Color>
<!-- 5. Mouse over background color for step buttons -->
<SolidColorBrush x:Key="MouseOverBackgroundBrush" Color="{DynamicResource MouseOverColor}"/>
<!-- 6. Background color active step -->
<SolidColorBrush x:Key="CheckedBackgroundBrush" Color="{DynamicResource MainThemeColor}"/>
<Style TargetType="{x:Type ToggleButton}" x:Key="ToggleButtonStyle">
<Setter Property="Background" Value="White" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="Width" Value="Auto"/>
<Setter Property="Height" Value="40"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="MinWidth" Value="80"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness ="{TemplateBinding BorderThickness}" Padding="5" Margin="2">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="{StaticResource CheckedBackgroundBrush}" />
<Setter Property="Background" Value="{StaticResource MouseOverBackgroundBrush}" />
<Setter Property="Foreground" Value="#333333" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="BorderBrush" Value="{StaticResource MouseOverBackgroundBrush}"/>
<Setter Property="Background" Value="{StaticResource CheckedBackgroundBrush}" />
<Setter Property="Foreground" Value="#ffffff"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<ToggleButton Style="{DynamicResource ToggleButtonStyle}">Button 1</ToggleButton>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}">Button 2</ToggleButton>
<ToggleButton Style="{DynamicResource ToggleButtonStyle}">Button 3</ToggleButton>
</StackPanel>
</Grid>
橙色
紫色
按钮1
按钮2
按钮3
使用矩形似乎很有效。 看看这个:。这对我来说毫无意义
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Background" Value="White" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="BorderBrush" Value="{DynamicResource MouseOverBackgroundBrush}"/>
<Setter Property="Width" Value="Auto"/>
<Setter Property="Height" Value="40"/>
<Setter Property="FontSize" Value="13"/>
<Setter Property="MinWidth" Value="80"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid>
<Rectangle Fill="{TemplateBinding Background}"
Stroke="{TemplateBinding BorderBrush}"
StrokeThickness="{TemplateBinding BorderThickness}"
Margin="2">
</Rectangle>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="2"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="{StaticResource CheckedBackgroundBrush}" />
<Setter Property="Background" Value="{StaticResource MouseOverBackgroundBrush}" />
<Setter Property="Foreground" Value="#333333" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="BorderBrush" Value="{StaticResource MouseOverBackgroundBrush}"/>
<Setter Property="Background" Value="{StaticResource CheckedBackgroundBrush}" />
<Setter Property="Foreground" Value="#ffffff"/>
</Trigger>
</Style.Triggers>
</Style>
由于画笔的颜色属性是使用动态资源标记扩展设置的,因此您还应该使用动态资源在setter中设置属性:
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource CheckedBackgroundBrush}" />
<Setter Property="Background" Value="{DynamicResource MouseOverBackgroundBrush}" />
<Setter Property="Foreground" Value="#333333" />
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource MouseOverBackgroundBrush}"/>
<Setter Property="Background" Value="{DynamicResource CheckedBackgroundBrush}" />
<Setter Property="Foreground" Value="#ffffff"/>
</Trigger>
</Style.Triggers>
如果您使用
StaticResource
一旦在运行时实际查找了动态颜色资源,设置器的值将不会更新。您可以尝试使用WPF Inspector或Visual Studio 2015查看可视化树。在这里,您可以看到应用了哪些资源以及使用了哪些样式。我已经做到了。它只是说BorderBrush是“透明的”,它来自一个样式触发器。您可以尝试将BorderBrush setter设置为一个特定的值,而不是引用静态资源。只是想看看这是否有效,它确实有效。我想在我的初始示例中提到这一点,但我不想让它太混乱。然后您可以尝试使用StaticResource而不是DynamicSource,如
。调试时,如果找不到资源,您将在VS的输出窗格中收到错误消息。这也适用于边框
。仍然奇怪的是,另一种方法使用的是矩形
,它使用的是StaticResource和DynamicSource的组合,而不是BorderBrush。**编辑,我的错,我只是有一个错误的名称。它也可以与资源字典一起使用