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.