WPF brightness change

Well, I have a black and white application, and I need a function to lower the brightness, how can I do this? all white comes from SolidColorBrush, which is stored in ResourceDictionary (Application.xaml), my current solution is to put an empty window that returns with an opacity of 80%, but this does not allow me to use the base window.

+4
source share
3 answers

If all the user interface elements use the same Brush , why not just change the Brush to reduce the brightness? For instance:

 public void ReduceBrightness() { var brush = Application.Resources("Brush") as SolidColorBrush; var color = brush.Color; color.R -= 10; color.G -= 10; color.B -= 10; brush.Color = color; } 

Edit after comments in Brush :

If you use one of the built-in brushes (through the Brushes class), it will be frozen. Instead of using one of them, declare your Brush without freezing it:

 <SolidColorBrush x:Key="Brush">White</SolidColorBrush> 

Edit after Robert's comment on application level resources:

Robert is right. Resources added at the Application level are automatically frozen if they are frozen. Even if you explicitly ask them not to freeze:
 <SolidColorBrush x:Key="ForegroundBrush" PresentationOptions:Freeze="False" Color="#000000"/> 

There are two ways around this:

  • As Robert suggested, put the resource at a lower level in the resource tree. For example, in the Window Resources collection. This makes it difficult to share information.
  • Put the resource in a shell that is not hovering.

As an example of No. 2, consider the following.

App.xaml:

 <Application.Resources> <FrameworkElement x:Key="ForegroundBrushContainer"> <FrameworkElement.Tag> <SolidColorBrush PresentationOptions:Freeze="False" Color="#000000"/> </FrameworkElement.Tag> </FrameworkElement> </Application.Resources> 

Window1.xaml:

 <StackPanel> <Label Foreground="{Binding Tag, Source={StaticResource ForegroundBrushContainer}}">Here is some text in the foreground color.</Label> <Button x:Name="_button">Dim</Button> </StackPanel> 

Window1.xaml.cs:

 public partial class Window1 : Window { public Window1() { InitializeComponent(); _button.Click += _button_Click; } private void _button_Click(object sender, RoutedEventArgs e) { var brush = (FindResource("ForegroundBrushContainer") as FrameworkElement).Tag as SolidColorBrush; var color = brush.Color; color.R -= 10; color.G -= 10; color.B -= 10; brush.Color = color; } } 

It's not so pretty, but it's the best I can think of right now.

+5
source

Solving this by changing the opacity of my root element instead of trying to change the brush, but it would still be nice if someone told me if I can do it somehow or not.

0
source

Kent will work if SolidColorBrush added to resources at a lower level. Freezables automatically freeze when they are added to Application.Resources .

0
source

All Articles