2013-10-18 90 views
1

我有2個元素 - 窗口和按鈕。
我想將WindowBorderBrush值綁定到ButtonisPressedBackground值。WPF將Setter屬性值與另一個Setter屬性值以不同的樣式

這兩個元素都有自定義樣式。這裏的風格:
按鈕樣式:

<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

在您的按鈕ControlTemplate.TriggerIsPressed可以綁定BackgroundSetter像:

<Setter Property="Background" Value="{Binding BorderBrush, RelativeSource= {RelativeSource AncestorType={x:Type local:MetroWindow}}}"/> 
+0

我想完全相反。我不能將名稱設置爲窗口樣式,否則程序會崩潰 – Ron

+0

那更簡單...更新了答案 – Nitin

+0

我認爲RelativeSource和AncestorType都參與其中,但是不知道在那裏寫什麼,謝謝! – Ron

0

在您的Button ControlTemplate.TriggerIsPressed你可以綁定在Setter Background像:

Setter Property="Background" Value="{Binding Path=BorderBrush, RelativeSource={RelativeSource TemplatedParent}}"