Thursday, November 10, 2016

To allocate static resources programmatically using c#

7:47 AM - By azeheruddin khan 0

Explanation: - The same Static Resources can be allocated pro grammatically within the event handler of an event like Window load. Brush is a class which is used to paint graphical objects on framework-level elements like Window or Page. This class belongs to System.Windows.Media  namespace. 

The  this keyword points to the current Framework-level element i.e. Window. Find Resource (object resource Key) method of Framework Element class searches for a resource based on its specified key, and throws an exception, if the requested resource is not found.


<Window x:Class="DynamicResources.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <ImageBrush x:Key="image" ImageSource="c:\AJAY DESAI JPAL PHOTO.jpg"></ImageBrush>
        <SolidColorBrush x:Key="BrownBrush" Color="Brown"></SolidColorBrush>
        <TextBox Name="txt" FontSize="20" FontStyle="Italic" FontFamily="Times New Roman" Margin="117,12,139,119"></TextBox>
        <Button Name="formatchange" Margin="42,241,310,36" Click="formatchange_Click" Content="SHOW"></Button>


using System.Windows;
using System.Windows.Media;

namespace DynamicResources
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()

        private void formatchange_Click(object sender, RoutedEventArgs e)
            txt.Background = (Brush)this.FindResource("image");
            txt.Foreground = (Brush)this.FindResource("BrownBrush");


About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, and ms sql.My work comprise of medium and enterprise level projects using and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post



© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top