在XAML中将一个属性值设置为另一个属性值

huangapple go评论87阅读模式
英文:

Setting a Property value to another property value in XAML

问题

我是新手,想要在悬停在按钮上方时将按钮的background更改为BorderBrush的颜色。

以下是代码:

<Style TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        CornerRadius="15">
                    <ContentPresenter HorizontalAlignment="Center"
                                      VerticalAlignment="Center"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" 
                    Value="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"/>
            <Setter Property="Cursor" Value="Hand"/>
        </Trigger>
    </Style.Triggers>
</Style>

光标的值会按预期更改,但背景色却没有。

我还尝试了RelativeSource={RelativeSource {x:Type Button}},但没有成功。

英文:

I am new to XAML, I want to change the background of a button to the color of the BorderBrush when I hover above it

Here's the code:

&lt;Style TargetType=&quot;Button&quot;&gt;
            &lt;Setter Property=&quot;Template&quot;&gt;
                &lt;Setter.Value&gt;
                    &lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
                        &lt;Border Background=&quot;{TemplateBinding Background}&quot;
                        BorderBrush=&quot;{TemplateBinding BorderBrush}&quot;
                        BorderThickness=&quot;{TemplateBinding BorderThickness}&quot;
                        CornerRadius=&quot;15&quot;&gt;
                            &lt;ContentPresenter HorizontalAlignment=&quot;Center&quot;
                                      VerticalAlignment=&quot;Center&quot;/&gt;
                        &lt;/Border&gt;
                    &lt;/ControlTemplate&gt;
                &lt;/Setter.Value&gt;
            &lt;/Setter&gt;
            &lt;Style.Triggers&gt;
                &lt;Trigger Property=&quot;IsMouseOver&quot; Value=&quot;True&quot;&gt;
                    &lt;Setter Property=&quot;Background&quot; 
                    Value=&quot;{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type Button}}}&quot;/&gt;
                    &lt;Setter Property=&quot;Cursor&quot; Value=&quot;Hand&quot;/&gt;
                &lt;/Trigger&gt;
            &lt;/Style.Triggers&gt;
        &lt;/Style&gt;

The value of the Cursor changes as it should, the Background however doesn't.

I also tried RelativeSource={RelativeSource {x:Type BUtton}} but it didn't work.

答案1

得分: 0

你必须使用RelativeSource.Self

<Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background" 
            Value="{Binding BorderBrush, RelativeSource={RelativeSource Self}}"/>
    <Setter Property="Cursor" Value="Hand"/>
</Trigger>

但要注意,如果直接为按钮分配背景,例如:

<Button Background="Transparent" .../>

那么样式触发器中的背景设置将被忽略。在这种情况下,将触发器移到控件模板中,并使用RelativeSource.TemplatedParent

<Style TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="border"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        CornerRadius="15">
                    <ContentPresenter HorizontalAlignment="Center"
                                      VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="border"
                                Property="Background" 
                                Value="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}"/>
                        <Setter TargetName="border"
                                Property="Cursor"
                                Value="Hand"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
英文:

You have to use RelativeSource.Self:

&lt;Trigger Property=&quot;IsMouseOver&quot; Value=&quot;True&quot;&gt;
    &lt;Setter Property=&quot;Background&quot; 
            Value=&quot;{Binding BorderBrush, RelativeSource={RelativeSource Self}}&quot;/&gt;
    &lt;Setter Property=&quot;Cursor&quot; Value=&quot;Hand&quot;/&gt;
&lt;/Trigger&gt;

Be aware however that the Background Setter in the Style Trigger would be ignored if your directly assign the Button's Background like e.g.

&lt;Button Background=&quot;Transparent&quot; .../&gt;

In that case move the Trigger to the ControlTemplate, with RelativeSource.TemplatedParent:

&lt;Style TargetType=&quot;Button&quot;&gt;
    &lt;Setter Property=&quot;Template&quot;&gt;
        &lt;Setter.Value&gt;
            &lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
                &lt;Border x:Name=&quot;border&quot;
                        Background=&quot;{TemplateBinding Background}&quot;
                        BorderBrush=&quot;{TemplateBinding BorderBrush}&quot;
                        BorderThickness=&quot;{TemplateBinding BorderThickness}&quot;
                        CornerRadius=&quot;15&quot;&gt;
                    &lt;ContentPresenter HorizontalAlignment=&quot;Center&quot;
                                      VerticalAlignment=&quot;Center&quot;/&gt;
                &lt;/Border&gt;
                &lt;ControlTemplate.Triggers&gt;
                    &lt;Trigger Property=&quot;IsMouseOver&quot; Value=&quot;True&quot;&gt;
                        &lt;Setter TargetName=&quot;border&quot;
                                Property=&quot;Background&quot; 
                                Value=&quot;{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}&quot;/&gt;
                        &lt;Setter TargetName=&quot;border&quot;
                                Property=&quot;Cursor&quot;
                                Value=&quot;Hand&quot;/&gt;
                    &lt;/Trigger&gt;
                &lt;/ControlTemplate.Triggers&gt;
            &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;

huangapple
  • 本文由 发表于 2023年8月9日 11:39:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定