0

当我尝试使用我在应用程序中的多个按钮上创建的按钮样式时,发生了一些奇怪的事情。我为播放和暂停按钮创建了 2 个按钮模板。我根据播放状态更改样式。有超过 1 个具有播放/暂停按钮的窗口。由于某种原因,除了一个按钮之外,多边形消失了,但其余的样式(颜色、按下的颜色等)仍然有效。

我正在设置按钮样式,例如:

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

2 回答 2

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>
于 2013-08-20T14:31:27.357 回答
0

我认为错误在于您将Content属性设置为不是Freezable. 这意味着 WPF 将无法轻松地将其复制到Style. 如果您只是用ContentPresenter您在 中设置的任何内容替换您的Content Setter,它应该可以工作。

于 2013-08-19T20:31:51.470 回答