【发布时间】:2014-07-16 16:38:07
【问题描述】:
我有一个带有标签的简单 WPF 窗口。窗口中有一个默认样式,当鼠标悬停在标签上时,它会将标签背景更改为红色。很简单。
只要我不在任何地方将标签颜色设置/修改为任何东西,它就可以工作。
- 如果我在后面的代码中设置标签背景(通过下面代码中的“设置标签背景”按钮),样式触发器将停止工作
- 如果我在 XAML 中设置标签背景(未显示,但可以轻松编辑),则样式不起作用。
- 如果在后面的代码中设置标签颜色后,我尝试将背景恢复为原来的状态(无论是空的还是透明的),触发器就会从工作变为不工作。
我不明白为什么设置背景似乎会覆盖样式。
无论我为标签设置什么背景,我更愿意这样做是保持样式处于活动状态 - 如果我将鼠标移到上方,它应该将颜色更改为红色,无论我是否将其设置为蓝色或其他任何东西。如果我必须在后面的代码中创建触发器,我可以。
相反,似乎只是通过为标签分配背景颜色,我覆盖了样式。
这是 XAML:
<Grid Background="Black">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" Margin="10,10,10,10" Height="75" Click="Button_Set_Click">Set Label Background</Button>
<Button Grid.Column="0" Grid.Row="1" Margin="10,10,10,10" Height="75" Click="Button_Clear_Click">Clear Label Background</Button>
<Label Name="TestLabel" Grid.Row="2" Margin="10,10,10,10" BorderBrush="Gray" BorderThickness="5"></Label>
</Grid>
下面是代码:
public partial class WpfProblems : Window
{
public WpfProblems()
{
InitializeComponent();
}
private void Button_Clear_Click(object sender, RoutedEventArgs e)
{
// revert the label background color - I've tried setting to null, transparent also
// but the trigger never comes back.
TestLabel.Background = (Brush)new BrushConverter().ConvertFrom("#00FFFFFF");
}
private void Button_Set_Click(object sender, RoutedEventArgs e)
{
// Set the label background color
TestLabel.Background = Brushes.Blue;
}
}
【问题讨论】:
标签: wpf