1

我有以下按钮样式,其中有一个十字,用于关闭按钮TabItem

<Button Grid.Column="2" 
        Width="15" 
        Height="15" 
        HorizontalAlignment="Center" 
        VerticalAlignment="Center" 
        DockPanel.Dock="Right" 
        AttachedCommand:CommandBehavior.Event="Click"
        AttachedCommand:CommandBehavior.Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CloseWorkspaceCommand}">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid Background="{TemplateBinding Background}">
                            <Path x:Name="ButtonPath"
                                  Margin="3"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  Data="M0,0 L1,1 M0,1 L1,0" 
                                  SnapsToDevicePixels="True"
                                  Stretch="Uniform"
                                  Stroke="{DynamicResource CloseButtonStroke}"
                                  StrokeEndLineCap="Flat"
                                  StrokeStartLineCap="Flat"
                                  StrokeThickness="2"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabItem}}"
                                      Value="false" />
                                    <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabItem}}"
                                      Value="false" />
                                </MultiDataTrigger.Conditions>
                                <MultiDataTrigger.Setters>
                                    <Setter Property="Visibility" Value="Hidden" />
                                </MultiDataTrigger.Setters>
                            </MultiDataTrigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Visibility" Value="Hidden" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background"
                          Value="{DynamicResource CloseButtonBackgroundHighlighted}" />
                                <Setter TargetName="ButtonPath" 
                                          Property="Stroke"
                          Value="{DynamicResource CloseButtonStrokeHighlighted}" />
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Background"
                          Value="{DynamicResource CloseButtonBackgroundPressed}" />
                                <Setter TargetName="ButtonPath" 
                                          Property="Stroke"
                          Value="{DynamicResource CloseButtonStroke}" />
                                <Setter TargetName="ButtonPath" 
                                          Property="Margin"
                          Value="2.5,2.5,1.5,1.5" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
</Button>

这产生

例子

我想添加一个CheckButton样式以在关闭按钮旁边显示一个图钉,例如 VS2012。我可以做到这一点没问题,但我想把它放在CheckBox上面的同一个按钮模板内,这样当鼠标在该区域上时我会突出显示。问题是上面的按钮包含Path绘制“X”的按钮。

有没有我可以使上面的按钮样式通用,这样我就可以包含“X”PathCheckBox,而无需复制所有触发器的东西?

谢谢你的时间。

4

1 回答 1

1

我将通过创建像下面的 ToolBarButton 这样的按钮类并具有依赖属性 ButtonType 来解决此问题。我将在 ToolBarButton 上设置 ButtonType。

    public enum ButtonType
    {
        Close =0,
        Pin
    }
    public class ToolBarButton : Button
    {
        public ButtonType ButtonType
        {
            get { return (ButtonType)this.GetValue(ButtonTypeProperty); }
            set { this.SetValue(ButtonTypeProperty, value); }
        }
        public static readonly DependencyProperty ButtonTypeProperty = DependencyProperty.Register(
          "ButtonType", typeof(ButtonType), typeof(ToolBarButton), new PropertyMetadata(ButtonType.Close));
    }

在xml中:

<local:ToolBarButton ButtonType="Pin"/>

然后我将在样式中添加触发器以检查按钮类型并应用包含复选框的控件模板

               <Style.Triggers>
                    <Trigger Property="ButtonType" Value="Pin">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <CheckBox/>
                                </ControlTemplate>

                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </Style.Triggers>
于 2013-09-05T17:33:05.077 回答