当我尝试使用我在应用程序中的多个按钮上创建的按钮样式时,会发生一些奇怪的事情。我为播放和暂停按钮创建了2个按钮模板。我根据播放状态改为风格。有一个窗口有一个播放/暂停按钮。由于某种原因,多边形除了其中一个按钮外都会消失,但其余的样式(颜色,压制颜色等)仍然有效。
我正在设置按钮样式,如:
btnTelPlay.Style = (Style)FindResource("PlayButtonGreen");
btnTelPlay.Style = (Style)FindResource("GreenPause");
以下是样式:
<Style x:Key="PlayButtonGreen" TargetType="{x:Type Button}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Content">
<Setter.Value>
<Polygon Stroke="Black" StrokeThickness="1" Points="5,0 5,30, 30,15" Fill="White" />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border" CornerRadius="5" BorderThickness="1"
Background="#FF3DEE2B"
BorderBrush="#404040">
<ContentPresenter RecognizesAccessKey="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"
/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter TargetName="Border"
Property="BorderBrush" Value="#202020" />
</Trigger>
<Trigger Property="IsDefaulted" Value="true">
<Setter TargetName="Border"
Property="BorderBrush" Value="#202020" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border"
Property="Background" Value="#FF3DEE2B" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Border"
Property="Background" Value="#FF39C72B" />
<Setter TargetName="Border"
Property="BorderBrush" Value="#606060" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border"
Property="Background" Value="#EEEEEE" />
<Setter TargetName="Border"
Property="BorderBrush" Value="#AAAAAA" />
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="GreenPause" TargetType="{x:Type Button}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Content">
<Setter.Value>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Stretch">
<Rectangle Stroke="Black" StrokeThickness="1" Width="10" Height="30" Fill="White" Margin="0,0,2.5,0"></Rectangle>
<Rectangle Stroke="Black" StrokeThickness="1" Width="10" Height="30" Fill="White" Margin="2.5,0,0,0"></Rectangle>
</StackPanel>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border" CornerRadius="5" BorderThickness="1"
Background="#FF3DEE2B"
BorderBrush="#404040">
<ContentPresenter RecognizesAccessKey="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"
/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter TargetName="Border"
Property="BorderBrush" Value="#202020" />
</Trigger>
<Trigger Property="IsDefaulted" Value="true">
<Setter TargetName="Border"
Property="BorderBrush" Value="#202020" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border"
Property="Background" Value="#FF3DEE2B" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Border"
Property="Background" Value="#FF39C72B" />
<Setter TargetName="Border"
Property="BorderBrush" Value="#606060" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border"
Property="Background" Value="#EEEEEE" />
<Setter TargetName="Border"
Property="BorderBrush" Value="#AAAAAA" />
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:0)
我认为错误在于您将Content
属性设置为非Freezable
的属性。这意味着WPF无法轻松将其复制到Style
的新实例。如果您只是将ContentPresenter
替换为Content
Setter
中设置的任何内容,那么它应该有效。
答案 1 :(得分:0)
感谢Abe Heidebrecht,你引导我找到了我想要的结果的正确方法:
而不是:
<Setter Property="Content">
<Setter.Value>
<Polygon Stroke="Black" StrokeThickness="1" Points="5,0 5,30, 30,15" Fill="White" />
</Setter.Value>
</Setter>
我从<Setter Property="Content">
中删除了它并将其放在ControlTemplate中,如:
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border" CornerRadius="5" BorderThickness="1"
Background="#FF3DEE2B"
BorderBrush="#404040">
<Polygon Stroke="Black" StrokeThickness="1" Points="5,0 5,30, 30,15" Fill="White" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
例如,这里是一个已完成的样式/模板:
<Style x:Key="PlayButtonGreen" TargetType="{x:Type Button}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border" CornerRadius="5" BorderThickness="1"
Background="#FF3DEE2B"
BorderBrush="#404040">
<Polygon Stroke="Black" StrokeThickness="1" Points="5,0 5,30, 30,15" Fill="White" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsKeyboardFocused" Value="true">
<Setter TargetName="Border"
Property="BorderBrush" Value="#202020" />
</Trigger>
<Trigger Property="IsDefaulted" Value="true">
<Setter TargetName="Border"
Property="BorderBrush" Value="#202020" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border"
Property="Background" Value="#FF3DEE2B" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Border"
Property="Background" Value="#FF39C72B" />
<Setter TargetName="Border"
Property="BorderBrush" Value="#606060" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border"
Property="Background" Value="#EEEEEE" />
<Setter TargetName="Border"
Property="BorderBrush" Value="#AAAAAA" />
<Setter Property="Foreground" Value="#888888"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>