Simple way to avoid hard coded strings in xaml

3 minute read

Hard coded strings are annoying,
In XAML, we have a lot of cases of hard coded strings.

Here is a simple way of implementing static naming class, and use it instead of hard coded strings.

Suppose this is my xaml:

<Window x:Class="How.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:How"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type Button}" x:Key="LazyGuyMeaninglessName">
<Setter Property="Margin" Value="30"/>
<Setter Property="Height" Value="30"/>
<Setter Property="Width" Value="200"/>
</Style>
</Window.Resources>
<StackPanel>
<Button Click="Button_Click">Click me to activate my style</Button>
<Button Style="{DynamicResource LazyGuyMeaninglessName}"/>
</StackPanel>
</Window>


This is code behind:

private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button != null)
{
button.Style = (Style)this.FindResource("LazyGuyMeaninglessNameWithMistake");
}
}

I have a mistake in my code behind, and the program will crash, not so fun, and not simple to test this kind of stuff.

There is a very simple solution, Lets define this class:

public static class StyleNames
{
public static string ButtonStyleName = "Some Hard To write style name";
}

The benefits of using this class instead of hard coded strings are:

  • No need to avoid long meaningful names, cause you have auto complete built in the IDE.
  • The obvious: avoid type mistakes, and unexpected crashes.
  • Make your programmers less lazy, they'll be able to change the resource name in a single place, and even his member name if needed, VS will rename it in all references and in XAML(VS 2010) (with a "Ctrl + ." after editing the member name).
  • If the style name is not readonly(like in my sample), I'm sure there is a simple way to support theming and dynamic style changing, by just assigning new names for all the resource names inside StyleNames class, and telling the app to do something that will reload all styles, I'm not sure about it, so don't count on it.
  • No crashes of "could not find resource bla bla".

Here is the code after the use of static class:

<Window x:Class="How.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:How"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="{x:Type Button}" x:Key="{x:Static local:StyleNames.ButtonStyleName}">
<Setter Property="Margin" Value="30"/>
<Setter Property="Height" Value="30"/>
<Setter Property="Width" Value="200"/>
</Style>
</Window.Resources>
<StackPanel>
<Button Click="Button_Click">Click me to activate my style</Button>
<Button Style="{DynamicResource ResourceKey={x:Static local:StyleNames.ButtonStyleName}}"/>
</StackPanel>
</Window>

private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button != null)
{
button.Style = (Style)this.FindResource(StyleNames.ButtonStyleName);
}
}

Enjoy :)

Comments