Tony Vitabile
Tony Vitabile

Reputation: 8594

Need advice on how to change colors

I'm building a WPF application that will be run on laptop computers mounted in police cars. The app has to have a "Night Mode" which will use darker colors to be less of a strain on the eyes at night.

Since the application is going to be used while the officer is driving, I've increased the size of all of the controls and I've defined new default templates for things like comboboxes. This is to make the screen easier to read at a glance and to make it easier to hit controls on the touch screen when you have sausage fingers, like I do.

I've created an enumeration called TimesOfDay. There are two values in the enumeration, DayTime and NightTime. Each control has a DepenencyProperty called TimeOfDay of the TimesOfDay enumeration type. There's a button on the main screen that you push to change the value of the TimeOfDay property. When you click the button, it cycles the TimeOfDay property's value between the two values.

I'm still pretty new to WPF, so I'm not sure how to go about this, on the Xaml side. I think what I need to do is create two named styles, for example one called DayStyle and another called NightStyle. Then I need to add triggers somewhere to change the style applied to the controls when the TimeOfDay property changes. Is that right?

Do I just change the background and foreground colors of the controls by type? Can I do it by element name?

I'm very fuzzy on all of this. Any help would be appreciated.

Tony

Upvotes: 0

Views: 343

Answers (3)

Thomas Levesque
Thomas Levesque

Reputation: 292425

I wouldn't duplicate the style, because I hate duplicated code... You could easily achieve that with a trigger in the ControlTemplate:

<Trigger Property="TimeOfDay" Value="NightTime">
    <Setter TargetName="someControl" Property="Background" Value="Black" />
    <Setter TargetName="someOtherControl" Property="ForeGround" Value="Yellow" />
    ...
</Trigger>

Another option is to use the technique I described here. This way you don't even need to put the TimeOfDay information on the control itself, it can be an ambient property.

Upvotes: 0

myermian
myermian

Reputation: 32515

Follow this guide: http://weblogs.asp.net/psheriff/archive/2009/12/01/load-resource-dictionaries-at-runtime-in-wpf.aspx

  1. Create various xaml resource files, but make sure the file does not compile and copies into the bin directory instead.
  2. Decorate your xaml controls with DynamicResources.
  3. Load in your resources through code.

Basically, you are looking to "skin" your application. The code that loads in your resource file can take advantage of the TimeOfDay enumeration.

If you want it automated you can even have some static class that has a timer to automatically attempt to change the resource and set the timer on the application startup. :)

Upvotes: 0

brunnerh
brunnerh

Reputation: 184516

Each control has a DepenencyProperty called TimeOfDay of the TimesDay enumeration type.

Don't do that, just create two complete themes in separate ResourceDictionaries which you then can switch via the MergedDictionaries in the Application.Resources. There is no need to put the day-time information on the controls.

Upvotes: 1

Related Questions