In WPF, is it possible for XAML to get the color of the currently rendered background?

I have a control that displays a progress bar.

I want the progress bar color to be effectively a stronger version of the currently colored background, like so:

  • If the background is light yellow, I want the progress bar to be strong yellow.
  • If the background is light green, I want the progress bar to be strong green.
  • and etc.

Is this possible in WPF?

Note that I have no idea who sets the background color for me, so I cannot set it manually.

Update

I should clarify that some of the parent controls are set Background

to transparent

in XAML.

However, as far as the visual tree is concerned, it simply means that it Background

is passed on to all children.

+3


source to share


2 answers


If you know what type of controls you bind to it using RelativeSource. Then, based on the extracted brush, adjust it to your needs.

The first approach using a converter is as follows:

 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}}"/>

      

or the latter is only suitable using 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>

      

+3


source


You can use the general style progress bar as follows and bind the foreground color in the corresponding ViewModel. vm: ProgressBar is a usercontrol



 <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>

      

+1


source







All Articles