2

我有一个这样的按钮

    <Button Content="Add">
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type Button}">
                                    <StackPanel Orientation="Horizontal">
                                    <Image x:Name="image" Source="../Resources/Images/Icons/Pruefplan_Add_32_gray.png" Margin="8" />
                                        <TextBlock  x:Name="text" Text="ADD" Margin="3,0,3,0" VerticalAlignment="Center"></TextBlock>
                                    </StackPanel>
                                    <ControlTemplate.Triggers>
                                        <Trigger Property="IsMouseOver" Value="true">
                                            <Setter TargetName="image" Property="Source" Value="../Resources/Images/Icons/Pruefplan_Add_32.png" />
                                            <Setter TargetName="text" Property="Opacity" Value=".5" />
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Button.Style>
            </Button>

这个按钮有一个样式和一个触发器来改变鼠标悬停上的图像。一切正常。但我想为按钮使用全局资源。

    <Style x:Key="ButtonsMenue1" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#02FFFFFF" />

    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Padding" Value="1" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">

        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">

                <Grid Background="{TemplateBinding Background}">

                    <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                              Margin="10"
                                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                              RecognizesAccessKey="True"
                                                              TextBlock.FontFamily="Segoe WP Light"
                                                              TextBlock.Foreground="Black"
                                                              TextBlock.FontSize="14"

                                                              />

                    <Rectangle x:Name="border" 
                                                           SnapsToDevicePixels="True"
                                                           IsHitTestVisible="False" 
                                                           Opacity="0.25"
                                                           Width="10" Height="20" VerticalAlignment="Stretch" HorizontalAlignment="Right" 
                                                           Fill="DarkGray" Margin="15,0,0,0" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="contentPresenter" Property="Opacity" Value="1" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="False">
                        <Setter TargetName="contentPresenter" Property="Opacity" Value=".5" />
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="true" />
                    <Trigger Property="ToggleButton.IsChecked" Value="true" />
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="White" />
                    </Trigger>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
                        <Setter TargetName="border" Property="Rectangle.Width" Value="2"/>
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

</Style>

我知道我可以使用

    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonsMenue1}">

但它不会显示例如我的矩形作为分隔符。而且看起来不太好。你有一个不错的解决方案吗?美好的愿望

曼努埃尔

4

1 回答 1

1

不幸的是,您不能继承ControlTemplate. 您为继承样式定义的模板将覆盖您为基本样式定义的模板。

如果你想使用一个基本模板然后想修改它,你必须找到一种方法来使用属性。您将不得不定义一个具有 DependencyProperty 的自定义控件来处理特殊要求。就像是:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonsMenue1}">
    <Setter Property="ImageForMouseOver" Value="..." />
</Style>
于 2013-01-11T10:14:25.497 回答