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

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

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:

  1. <Style TargetType="Button">
  2. <Setter Property="Template">
  3. <Setter.Value>
  4. <ControlTemplate TargetType="Button">
  5. <Border Background="{TemplateBinding Background}"
  6. BorderBrush="{TemplateBinding BorderBrush}"
  7. BorderThickness="{TemplateBinding BorderThickness}"
  8. CornerRadius="15">
  9. <ContentPresenter HorizontalAlignment="Center"
  10. VerticalAlignment="Center"/>
  11. </Border>
  12. </ControlTemplate>
  13. </Setter.Value>
  14. </Setter>
  15. <Style.Triggers>
  16. <Trigger Property="IsMouseOver" Value="True">
  17. <Setter Property="Background"
  18. Value="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"/>
  19. <Setter Property="Cursor" Value="Hand"/>
  20. </Trigger>
  21. </Style.Triggers>
  22. </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:

  1. <Style TargetType="Button">
  2. <Setter Property="Template">
  3. <Setter.Value>
  4. <ControlTemplate TargetType="Button">
  5. <Border Background="{TemplateBinding Background}"
  6. BorderBrush="{TemplateBinding BorderBrush}"
  7. BorderThickness="{TemplateBinding BorderThickness}"
  8. CornerRadius="15">
  9. <ContentPresenter HorizontalAlignment="Center"
  10. VerticalAlignment="Center"/>
  11. </Border>
  12. </ControlTemplate>
  13. </Setter.Value>
  14. </Setter>
  15. <Style.Triggers>
  16. <Trigger Property="IsMouseOver" Value="True">
  17. <Setter Property="Background"
  18. Value="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"/>
  19. <Setter Property="Cursor" Value="Hand"/>
  20. </Trigger>
  21. </Style.Triggers>
  22. </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

  1. <Trigger Property="IsMouseOver" Value="True">
  2. <Setter Property="Background"
  3. Value="{Binding BorderBrush, RelativeSource={RelativeSource Self}}"/>
  4. <Setter Property="Cursor" Value="Hand"/>
  5. </Trigger>

但要注意,如果直接为按钮指定背景色,如:

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

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

  1. <Style TargetType="Button">
  2. <Setter Property="Template">
  3. <Setter.Value>
  4. <ControlTemplate TargetType="Button">
  5. <Border x:Name="border"
  6. Background="{TemplateBinding Background}"
  7. BorderBrush="{TemplateBinding BorderBrush}"
  8. BorderThickness="{TemplateBinding BorderThickness}"
  9. CornerRadius="15">
  10. <ContentPresenter HorizontalAlignment="Center"
  11. VerticalAlignment="Center"/>
  12. </Border>
  13. <ControlTemplate.Triggers>
  14. <Trigger Property="IsMouseOver" Value="True">
  15. <Setter TargetName="border"
  16. Property="Background"
  17. Value="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}"/>
  18. <Setter TargetName="border"
  19. Property="Cursor"
  20. Value="Hand"/>
  21. </Trigger>
  22. </ControlTemplate.Triggers>
  23. </ControlTemplate>
  24. </Setter.Value>
  25. </Setter>
  26. </Style>
英文:

You have to use RelativeSource.Self:

  1. &lt;Trigger Property=&quot;IsMouseOver&quot; Value=&quot;True&quot;&gt;
  2. &lt;Setter Property=&quot;Background&quot;
  3. Value=&quot;{Binding BorderBrush, RelativeSource={RelativeSource Self}}&quot;/&gt;
  4. &lt;Setter Property=&quot;Cursor&quot; Value=&quot;Hand&quot;/&gt;
  5. &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.

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

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

  1. &lt;Style TargetType=&quot;Button&quot;&gt;
  2. &lt;Setter Property=&quot;Template&quot;&gt;
  3. &lt;Setter.Value&gt;
  4. &lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
  5. &lt;Border x:Name=&quot;border&quot;
  6. Background=&quot;{TemplateBinding Background}&quot;
  7. BorderBrush=&quot;{TemplateBinding BorderBrush}&quot;
  8. BorderThickness=&quot;{TemplateBinding BorderThickness}&quot;
  9. CornerRadius=&quot;15&quot;&gt;
  10. &lt;ContentPresenter HorizontalAlignment=&quot;Center&quot;
  11. VerticalAlignment=&quot;Center&quot;/&gt;
  12. &lt;/Border&gt;
  13. &lt;ControlTemplate.Triggers&gt;
  14. &lt;Trigger Property=&quot;IsMouseOver&quot; Value=&quot;True&quot;&gt;
  15. &lt;Setter TargetName=&quot;border&quot;
  16. Property=&quot;Background&quot;
  17. Value=&quot;{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}&quot;/&gt;
  18. &lt;Setter TargetName=&quot;border&quot;
  19. Property=&quot;Cursor&quot;
  20. Value=&quot;Hand&quot;/&gt;
  21. &lt;/Trigger&gt;
  22. &lt;/ControlTemplate.Triggers&gt;
  23. &lt;/ControlTemplate&gt;
  24. &lt;/Setter.Value&gt;
  25. &lt;/Setter&gt;
  26. &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:

确定