I wanna share…

Using DynamicResource in WPF

DownLoad link: Click Here

DynamicResource: DynamicResources are resolved at runtime. Use DynamicResources when the value of the resource could change during the lifetime of the Application.

I will show you an example regarding DynamicResource.


<SolidColorBrush x:Key=”brush” Color=”BlueViolet”></SolidColorBrush>

<Style TargetType=”Border” x:Key=”background”>

<Setter Property=”Background” Value=”Green”></Setter>



In my WPF application page I’ve one textbox and two ellipses. In Window. Resources I defined the SolidColorBrush and the Style properties.

In above code you can observe x: Key attribute. x: Key uniquely identifies elements that are created and referenced in a XAML-defined dictionary.

Here I’m using above resources. Have a look at below code.

You can use this syntax in xaml design page: {DynamicResource x:keyName}



<Border Style=”{StaticResource background}”>

<DockPanel Height=”360″ Width=”312″>

<Button x:Name=”button” Content=”Click” Click=”Button_Click” Background=”CadetBlue” Height=”40″ Width=”63″ />





And in code behind page (.xaml.cs), I write this code in button click event.

private void Button_Click(object sender, RoutedEventArgs e)


this.button.SetResourceReference(BackgroundProperty, “brush”);


Run the page and you can see the color change of the button after you click on it.

Result window looks like this.

Hope you like this article;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: