1

我有 2 个元素 - 窗口和按钮。
我想将 ' 值绑定WindowBorderBrush'ButtonisPressed Background

这两个元素都有自定义样式。以下是样式:
按钮样式:

<Style x:Key="TitleBarButton" TargetType="Button">
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Padding" Value="12,8.5"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="FontFamily" Value="Marlett"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
                    <Grid>
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" TextBlock.FontFamily="{TemplateBinding FontFamily}" TextBlock.FontSize="{TemplateBinding FontSize}" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="#EFEFF2" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" Value="#007ACC"/>
                        <Setter Property="Foreground" Value="White"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

窗口样式:

<Style TargetType="{x:Type local:MetroWindow}">
    <Setter Property="Background" Value="White"/>
    <Setter Property="WindowStyle" Value="None"/>
    <Setter Property="ResizeMode" Value="CanResize"/>
    <Setter Property="AllowsTransparency" Value="True"/>
    <Setter Property="BorderBrush" Value="#007ACC"/>
    <Setter Property="BorderThickness" Value="0.5"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MetroWindow}">
                <Grid Name="BaseGrid" Background="{TemplateBinding Background}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="4" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="4" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="4" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="4" />
                    </Grid.ColumnDefinitions>
                    <DockPanel LastChildFill="true" Grid.RowSpan="3" Grid.ColumnSpan="3">
                        <Border Name="TopBorder" DockPanel.Dock="Top" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                        <Border Name="RightBorder" DockPanel.Dock="Right" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                        <Border Name="BottomBorder" DockPanel.Dock="Bottom" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                        <Border Name="LeftBorder" DockPanel.Dock="Left" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                        <DockPanel LastChildFill="True">
                            <DockPanel LastChildFill="True" DockPanel.Dock="Top">
                                <StackPanel Orientation="Horizontal" DockPanel.Dock="Right">
                                    <Button x:Name="Minimize" ToolTip="Minimize" Content="0" Style="{StaticResource TitleBarButton}"/>
                                    <Button x:Name="Restore" ToolTip="Restore" Content="2" Style="{StaticResource TitleBarButton}"  Visibility="Collapsed"/>
                                    <Button x:Name="Maximize" ToolTip="Maximize" Content="1" Style="{StaticResource TitleBarButton}"/>
                                    <Button x:Name="Close" ToolTip="Close" Content="r" Style="{StaticResource TitleBarButton}"/>
                                </StackPanel>
                                <DockPanel x:Name="TitleBar">
                                    <Border x:Name="IconBorder" Padding="5,0,0,0">
                                        <Image Width="16" Height="16" Source="{Binding Icon, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" RenderOptions.BitmapScalingMode="NearestNeighbor" RenderOptions.EdgeMode="Aliased"/>
                                    </Border>
                                    <Label x:Name="Title" VerticalAlignment="Center" FontSize="14" Content="{Binding Title, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
                                </DockPanel>
                            </DockPanel>
                            <Grid>
                                <AdornerDecorator>
                                    <ContentPresenter/>
                                </AdornerDecorator>
                            </Grid>
                        </DockPanel>
                    </DockPanel>
                    <Rectangle x:Name="TopLeftRectangle" Fill="Transparent" />
                    <Rectangle x:Name="TopRectangle" Fill="Transparent" Grid.Column="1" />
                    <Rectangle x:Name="TopRightRectangle" Fill="Transparent" Grid.Column="2" />
                    <Rectangle Name="RightRectangle" Fill="Transparent" Grid.Row="1" Grid.Column="2" />
                    <Rectangle Name="BottomRightRectangle" Fill="Transparent" Grid.Row="2" Grid.Column="2" />
                    <Rectangle Name="BottomRectangle" Fill="Transparent" Grid.Row="2" Grid.Column="1" />
                    <Rectangle Name="BottomLeftRectangle" Fill="Transparent" Grid.Row="2" />
                    <Rectangle Name="LeftRectangle" Fill="Transparent" Grid.Row="1" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
4

2 回答 2

5

In your Button ControlTemplate.Trigger for IsPressed you can bind the Background in Setter like:

    <Setter Property="Background" Value="{Binding BorderBrush, RelativeSource= {RelativeSource AncestorType={x:Type local:MetroWindow}}}"/>
于 2013-10-18T14:32:27.330 回答
0

在你的Button ControlTemplate.TriggerforIsPressed你可以绑定Backgroundin Setter 像:

Setter Property="Background" Value="{Binding Path=BorderBrush, RelativeSource={RelativeSource TemplatedParent}}"
于 2016-10-05T17:41:52.750 回答