我试图根据设置在xaml中的枚举更改我的标签的颜色。我无法获得更新的颜色。任何帮助都会很棒。WPF更改Xaml中基于代码隐藏属性的颜色
谢谢!
<UserControl.Resources>
<!-- Normal -->
<SolidColorBrush x:Key="Normal_bg_Unselect" Color="#FF1A73CC" />
<SolidColorBrush x:Key="Normal_fg_Unselect" Color="#FF72BAFF" />
<SolidColorBrush x:Key="Normal_bg_Select" Color="#FF1ACCBF" />
<SolidColorBrush x:Key="Normal_fg_Select" Color="#FF91FFFF" />
</UserControl.Resources>
<Grid>
<Label Name="BackgroundLabel" Width="Auto" Height="Auto" BorderThickness="0" Panel.ZIndex="1" Cursor="Hand">
<Label.Foreground>
<SolidColorBrush Color="{DynamicResource Color_LightBlue}"/>
</Label.Foreground>
<Label.Style>
<Style TargetType="{x:Type Label}">
<Setter Property="Background" Value="{Binding BgUnselect}" />
<Setter Property="Foreground" Value="{Binding FgUnselect}" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{Binding BgSelect}" />
<Setter Property="Foreground" Value="{Binding FgSelect}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Background" Value="{Binding BgUnselect}" />
<Setter Property="Foreground" Value="{Binding FgUnselect}" />
</Trigger>
</Style.Triggers>
</Style>
</Label.Style>
<Label.OpacityMask>
<LinearGradientBrush>
<GradientStop Color="#00FFFFFF" Offset="-.35"/>
<GradientStop Color="#FFFFFFFF" Offset="1"/>
</LinearGradientBrush>
</Label.OpacityMask>
</Label>
<TextBlock Name="ContentLabel" Text="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, FallbackValue='Styled Button'}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20,0,0,0" FontFamily="/HarringtonGroup.TrainingBuilder;component/Fonts/#HelveticaNeue" FontSize="30" Foreground="{Binding ElementName=BackgroundLabel, Path=Foreground}" />
</Grid>
代码
public SolidColorBrush BgUnselect { get; set; }
public SolidColorBrush FgUnselect { get; set; }
public SolidColorBrush BgSelect { get; set; }
public SolidColorBrush FgSelect { get; set; }
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
switch (ButtonType)
{
case ButtonType.Normal:
BgUnselect = (SolidColorBrush)FindResource("Normal_bg_Unselect");
FgUnselect = (SolidColorBrush)FindResource("Normal_fg_Unselect");
BgSelect = (SolidColorBrush)FindResource("Normal_bg_Select");
FgSelect = (SolidColorBrush)FindResource("Normal_fg_Select");
return;
case ButtonType.OK:
case ButtonType.Cancel:
return;
}
这工作,但是我想有3套的颜色为一个控制,我可以去按钮类型=正常,并获得normal_bg_unselect为背景。当我将ButtonType设置为Ok时,这将不起作用,因为颜色将设置为正常。 – Ryan 2010-06-28 15:29:51
只是为了澄清,这是一个包含标签的自定义Button控件? – SergioL 2010-06-28 15:54:30
这是正确的 – Ryan 2010-06-28 16:13:55