SIGN UP MEMBER LOGIN:    
ARTICLE

Using DynamicResources in WPF

Posted by Jitendra Sampathirao Articles | WPF with C# October 27, 2010
This article describes about DynamicResource with an example.
Reader Level:

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.

 

<Window.Resources>

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

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

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

</Style>

</Window.Resources>

 

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}

 

<Grid>

<StackPanel>

<Border Style="{StaticResource background}">

<DockPanel Height="360" Width="312">                

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

</DockPanel>

</Border>

</StackPanel>

</Grid>

 

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.

 

DynamicResource.bmp

 

In my very next article, I will explain you about DynamicResource with Animation.


Hope you like this article; let me know if you have any queries.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor