在WPF中,XAML是否可以获得当前渲染背景的颜色

本文关键字:背景 颜色 WPF XAML 是否 | 更新日期: 2023-09-27 18:00:54

我有一个显示进度条的控件。

我希望进度条的颜色有效地是当前渲染的背景颜色的更强版本,例如:

  • 如果背景是浅黄色,我希望进度条是强烈的黄色
  • 如果背景是浅绿色,我希望进度条是强绿色
  • 等等

这在WPF中可能吗?

请注意,我不知道谁在为我设置背景色,所以我真的无法手动设置。

更新

我应该澄清一下,某些父控件在XAML中将Background设置为transparent

然而,就视觉树而言,这只是意味着Background被传递给所有的子级。

在WPF中,XAML是否可以获得当前渲染背景的颜色

如果您知道什么类型的控件使用RelativeSource设置后台绑定。然后根据检索到的Brush调整它以满足您的需求。

使用Converter的第一种方法如下:

 class BackgroundConverter : IValueConverter
 {
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
         var background = value as SolidColorBrush;
         if (background.Color == Colors.LightYellow) return new SolidColorBrush(Colors.Yellow);
         if (background.Color == Colors.LightGreen) return new SolidColorBrush(Colors.Green);
         return new SolidColorBrush(Colors.White);
     }
     public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
         throw new NotImplementedException();
     }
}

XAML:

<Window.Resources>
    <local:BackgroundConverter x:Key="BackgroundConverter"/>
</Window.Resources>
<ProgressBar Background="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Background,
      Converter={StaticResource BackgroundConverter}}"/>

或后一种仅使用XAML的方法:

<ProgressBar>
    <ProgressBar.Style>
        <Style TargetType="ProgressBar">
            <Setter Property="Background" Value="White"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Background}" Value="LightYellow">
                    <Setter Property="Background" Value="Yellow"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=Background}" Value="LightGreen">
                    <Setter Property="Background" Value="Green"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ProgressBar.Style>
</ProgressBar>

您可以按如下通用样式使用进度条,并将前景色绑定到相应的ViewModel中。vm:ProgressBar是一个用户控制

 <vm:ProgressBar x:Name="ProgressBar"
                                        Grid.RowSpan="3"
                                        Width="140"
                                        Height="120"
                                        Margin="12"
                                        Panel.ZIndex="5"
                                        Padding="10"
                                       DataContext.ProgressBarVisibility,
                                                             RelativeSource={RelativeSource AncestorType=Window,
                                                                                            AncestorLevel=1}}"
                                        d:DesignerVisibility="False">
                            <vm:ProgressBar.Foreground>
                                <RadialGradientBrush Center="0.5,0.5" GradientOrigin="0.4,0.4" RadiusX="0.5" RadiusY="0.5">
                                    <RadialGradientBrush.GradientStops>
                                        <GradientStop Offset="0" Color="Transparent" />
                                        <GradientStop Offset="1" Color="{Binding Path=FColor}" />
                                    </RadialGradientBrush.GradientStops>
                                </RadialGradientBrush>
                            </vm:ProgressBar.Foreground>
                        </vm:ProgressBar>