英文:
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:
<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
:
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background"
Value="{Binding BorderBrush, RelativeSource={RelativeSource Self}}"/>
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
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.
<Button Background="Transparent" .../>
In that case move the Trigger to the ControlTemplate, with 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论