How to perform binding to data placed in Resources


Today I’ll show you how you can use static binding extension from the WPF to perform binding to data placed in resources.

Imagine that you have file that is named MyResources.resx which contains key-value pairs with text that is used throughout your application:


Note that access modifier is set to “public” in order to allow XAML parser to access those resources.

And here is XAML that can use to get data from resources collection:

<Window x:Class="BindingToResourcesSample.MainWindow" xmlns="" xmlns:x="" xmlns:local="clr-namespace:BindingToResourcesSample" Title="MainWindow" Height="350" Width="525"> <StackPanel> <Button Content="{x:Static local:MyResources.ButtonText}"/> <TextBlock Text="{x:Static local:MyResources.LabelText}"/> </StackPanel> </Window>

As you can see, you simply need to declare reference to namespace where file with resources is placed and access needed value via StaticResourceExtension.

That’s it. Remember to place text data into resources to prevent you from localization nightmare after years of developmentSmile.