按下按钮模板中的颜色交换

时间:2011-08-24 14:36:02

标签: wpf templates xaml triggers styles

我正在尝试设计一个按钮模板,按下时会交换颜色。

不幸的是我无法让它发挥作用。

我使用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>

任何帮助都将不胜感激。

关心塞巴斯蒂安

2 个答案:

答案 0 :(得分:3)

<强>更新

最后了解你真正想做的事情。这是你如何完成它。绑定到TemplatedParent的{​​{1}}和Background并在设置者中设置Foreground。这样,颜色的来源将始终保持不变,您可以轻松交换它们

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> 中的Bindings

Template

只是

Background="{Binding Path=Background,
                     RelativeSource={RelativeSource FindAncestor,
                                                    AncestorType=Button,
                                                    AncestorLevel=1}}"

答案 1 :(得分: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>

这是CodeBehind

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中 - 所以我不会将其标记为已回答。