2011-08-24 39 views
1

我試圖設計一個按鈕模板,它在按下時交換顏色。在按下按鈕模板時交換顏色

不幸的是我不能得到它的工作。

我正在使用BorderBrush作爲臨時變量。很可能有更復雜的解決方案。

這是我的代碼。

<Style TargetType="{x:Type Button}"> 
     <Style.Setters> 
      <Setter Property="Foreground" 
        Value="{StaticResource RahmenFarbe}" /> 
      <Setter Property="Background" 
        Value="{StaticResource HintergrundFarbe}" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <Grid Width="{TemplateBinding Width}" 
           Height="{TemplateBinding Height}" 
           ClipToBounds="True"> 
          <Border BorderBrush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Foreground}" 
            x:Name="ButtonBorder" 
            CornerRadius="25" 
            BorderThickness="4" 
            Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Background}" 
            RenderTransformOrigin="0.5,0.5"> 
           <TextBlock x:Name="ButtonTextBlock" 
              Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Foreground}" 
              VerticalAlignment="Center" 
              HorizontalAlignment="Center"> 
           <ContentPresenter x:Name="myContentPresenter" 
                Content="{TemplateBinding Content}" /> 
           </TextBlock> 
          </Border> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsPressed" 
            Value="True"> 
           <Setter Property="BorderBrush" 
             Value="{Binding Mode=OneTime, Path=Foreground}" /> 
           <Setter Property="Foreground" 
             Value="{Binding Mode=OneTime, Path=Background}" /> 
           <Setter Property="Background" 
             Value="{Binding Mode=OneTime, Path=BorderBrush}" /> 
          </Trigger> 
         </ControlTemplate.Triggers > 

        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style.Setters> 
    </Style> 

任何幫助,將不勝感激。

問候塞巴斯蒂安

回答

3

更新

終於明白自己真正想做的事。這是你如何完成它。綁定到TemplatedParentBackgroundForeground並在設置者中設置TargetName。這樣一來,對於顏色的源將始終保持不變,您可以輕鬆地交換他們

<ControlTemplate.Triggers> 
    <Trigger Property="IsPressed" Value="True"> 
     <Setter TargetName="ButtonBorder" 
       Property="Background" 
       Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Foreground}" /> 
     <Setter TargetName="ButtonTextBlock" 
       Property="Foreground" 
       Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" /> 
    </Trigger> 
</ControlTemplate.Triggers> 

或者,您也可以從

改變你 TemplateBindings
Background="{Binding Path=Background, 
        RelativeSource={RelativeSource FindAncestor, 
                AncestorType=Button, 
                AncestorLevel=1}}" 

要只是

Background="{TemplateBinding Background}" 
+0

我絕對同意。但我喜歡存檔的另一件事是,當我更改按鈕中的顏色時,行爲(交換)保持不變。這看起來很尷尬,如果硬編碼的顏色。我擔心我必須去找代碼(borderbrush剛剛被濫用作爲臨時變量) – Athanviel

+0

但是您已經在模板中將Foreground和Background設置爲靜態資源,它是如何被濫用的?如果你想使其動態化,可以使用'DynamicResource'來代替你需要的地方。另一種選擇是使用附加屬性。您將無法通過將前景設置爲背景來交換它們,因爲背景也在變化 –

+0

實際上,我將邊框的前景和背景綁定到按鈕的邊框。那些綁定應該是靜態的。在觸發器中,我更改按鈕屬性,所以其他綁定不應該受到影響。我試圖通過「綁定模式=一次」來避免自動更改。 DynamicResource沒有按照我的意圖工作,但也許我沒有正確使用它。 – Athanviel

0

這是帶有代碼隱藏的解決方案。

<Style TargetType="{x:Type Button}"> 
     <Style.Setters> 
      <Setter Property="Foreground" 
        Value="{StaticResource RahmenFarbe}" /> 
      <Setter Property="Background" 
        Value="{StaticResource HintergrundFarbe}" /> 
      <Setter Property="FontSize" 
        Value="18" /> 
      <Setter Property="FontWeight" 
        Value="Bold" /> 
      <EventSetter Event="PreviewMouseDown" 
         Handler="vertauscheFarben" /> 
      <EventSetter Event="PreviewMouseUp" 
         Handler="vertauscheFarben" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <Grid Width="{TemplateBinding Width}" 
           Height="{TemplateBinding Height}" 
           ClipToBounds="True"> 
          <Border BorderBrush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Foreground}" 
            x:Name="ButtonBorder" 
            CornerRadius="25" 
            BorderThickness="4" 
            Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Background}" 
            RenderTransformOrigin="0.5,0.5"> 
           <TextBlock x:Name="ButtonTextBlock" 
              Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Foreground}" 
              VerticalAlignment="Center" 
              HorizontalAlignment="Center"> 
           <ContentPresenter x:Name="myContentPresenter" 
                Content="{TemplateBinding Content}" /> 
           </TextBlock> 
          </Border> 
         </Grid> 

        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style.Setters> 
    </Style> 

這是代碼隱藏

Private Sub vertauscheFarben(sender As Object, e As MouseButtonEventArgs) 
    Dim Button = DirectCast(sender, Button) 

    Dim Temp = Button.Foreground 
    Button.Foreground = Button.Background 
    Button.Background = Temp 
End Sub 

這是工作,但我會內的XAML喜歡它 - 所以我不會慶祝這個作爲回答。