Sunday, 2 August 2015

Overriding the windows theme in WPF

WPF applications are styled by default to match the current Windows theme. If you want your application to use another Windows theme, simply override the default theme in the App.xaml file of your project.

This is what a simple application window looks like by default on Windows 8:



In order to override the look of your application so that it resembles the Windows 7 theme, simply change your App.xaml to add this to your Application.Resources section

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
   <ResourceDictionary Source = "/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/>
   </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>


Next, add a reference to the PresentationFramework dll from the Solution Explorer (right click on references):


Select the PresentationFramework.Aero dll from the list:



Run your application.
Your application will now look like this:



The downside is that the window (border) does not seem to be affected by this.

The other Windows themes can also be selected (Eg: Luna). Just select the appropriate dll reference and update the code highlighted in red above to match it. The other options can be seen in the image showing the selection of the reference to be added.

Sunday, 12 July 2015

(WPF) Check(events) if the user has switched to High contrast mode in C#

When users use high contrast modes on Windows, hardcoded colors in Windows Presentation Foundation applications result in an inconsistent(often ugly and not very usable) look. Changing colors or themes dynamically when the switch is made is sometimes required(although rarely, if you design your styles well).

For those like me who prefer doing this in C# rather than XAML, the SystemParameters.HighContrast property provides a boolean value that is True or False depending on whether one of the high-contrast modes is enabled. This property can be checked by an event handler that is registered to the SystemParameters.StaticPropertyChanged event.

One caveat is that while the SystemParameters.HighContrast property is available on .NET versions 3 and up, the StaticPropertyChanged .event is only available from version 4.5.
For applications that support .NET versions below 4.5, simply subscribe to the SystemEvents.UserPreferenceChanged event instead. For example usage, see my previous post (lines 47 and 63).

Windows Presentation Foundation TextBox with placeholder/hint/watermark

WPF does not ship with a textbox control which has support for a watermark aka placeholder aka hint.

This is my custom control. By no means is this the most efficient or tiniest solution. There are probably better. This is intended for learners because it does not require XAML(XML) Triggers or styles. Just plain old C# with a dependency property and event handling.


Friday, 23 January 2015

Guava dependency error while installing M2Eclipse on Eclipse Juno

Quick update:

Recently I was working on Eclipse Juno, I encountered a problem while trying to install M2Eclipse.
The eclipse software installation status complained to me that it could not find the artifact 'bundle com.google.guava...'.

While for some this may be a no-brainer, I have posted it here for those who think this could be a bug. This occurs when you try to install the 1.5 version of M2E. If you cannot download the latest version of Eclipse, you can simply settle for installing version 1.4 of M2E because the version of Guava that M2E 1.5 requires is not bundled with Eclipse Juno.

Monday, 19 January 2015

A simple bottle app for listing directory contents (python 3)

Here is a simple directory listing application created using the bottle.py micro framework:
(Please notify me of any problems with my code using my github presence or in the comments).