【问题标题】:WPF Change color in Xaml based off code behind propertyWPF 在 Xaml 中基于属性后面的代码更改颜色
【发布时间】:2010-06-28 15:02:37
【问题描述】:

我正在尝试根据 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;
        }

【问题讨论】:

    标签: wpf xaml binding


    【解决方案1】:

    您的绑定标签不完整,您必须定义RelativeSource 或ElementName

    如下更改您的用户控件

    <UserControl x:Name="userControl"
    

    并将绑定应用为,

    Value="{Binding BgSelect, ElementName=userControl}"
    

    默认情况下,绑定查找 BgSelect 作为用户控件的“DataContext”属性的属性。

    此外,由于 UserControl 是从 DependencyObject 派生的,除非您的属性 BgSelect 等是依赖属性,否则这将不起作用。

    【讨论】:

      【解决方案2】:

      在我看来,您要做的就是将 Foreground 和 Background 属性设置为您定义的资源。

      您是否尝试用{StaticResource ...} 替换您的{Binding ...} 代码?

      例如,改变

      <Setter Property="Background" Value="{Binding BgUnselect}" /> 
      

      <Setter Property="Background" Value="{StaticResource Normal_bg_Unselect}" />
      

      编辑下方(根据评论)

      您可以使用样式来控制每种按钮类型的 4 种颜色。我创建了一个小例子,你可以将它应用到你的代码中。如果不清楚,我会尝试为您的代码示例重写。

      创建基本样式:

      <Style x:Key="LabelStyleBase" TargetType="{x:Type Label}">
          <Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}"/>
          <Setter Property="Background" Value="{DynamicResource BackgroundBrush}"/>
              <!-- more style settings -->
      </Style>
      

      然后创建您的变体:

      <Style x:Key="LabelStyle1" BasedOn="{StaticResource LabelStyleBase}" TargetType="{x:Type Label}">
          <Style.Resources>
              <SolidColorBrush x:Key="ForegroundBrush" Color="Purple" />
              <SolidColorBrush x:Key="BackgroundBrush" Color="Pink" />    
          </Style.Resources>
      </Style> 
      
      <Style x:Key="LabelStyle2" BasedOn="{StaticResource LabelStyleBase}" TargetType="{x:Type Label}">
          <Style.Resources>
              <SolidColorBrush x:Key="ForegroundBrush" Color="Aqua" />
              <SolidColorBrush x:Key="BackgroundBrush" Color="Yellow" />  
          </Style.Resources>
      </Style>
      

      您可能会收到无法找到资源的警告,但这应该没问题。

      替代解决方案

      最后,如果您不想走这条路,您可能必须在类上实现 INotifyPropertyChanged 并重写画笔属性上的设置器以触发 NotifyPropertyChanged 事件。

      有点不清楚您究竟是如何实现自定义 Button 控件的,但您可能应该将按钮类型枚举公开为 DependencyProperty,并在 DependencyProperty 的更改通知上更改颜色画笔。

      希望对您有所帮助。

      【讨论】:

      • 这行得通,但是我想为一个控件设置 3 组颜色,我可以使用 ButtonType = Normal 并将 normal_bg_unselect 作为背景。当我将 ButtonType 设置为 Ok 时,这将不起作用,因为颜色将设置为正常。
      • 澄清一下,这是一个包含标签的自定义 Button 控件?
      猜你喜欢
      • 1970-01-01
      • 2010-10-07
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 2010-12-30
      • 1970-01-01
      相关资源
      最近更新 更多