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

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

Setting a Property value to another property value in XAML

问题

以下是翻译好的部分:

"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:

<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>

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."

英文:

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:

<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>

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-2.html
匿名

发表评论

匿名网友

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

确定