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.


MainWindow.xaml

<Window x:Class="DynamicResources.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ImageBrush x:Key="image" ImageSource="c:\AJAY DESAI JPAL PHOTO.jpg"></ImageBrush>
        <SolidColorBrush x:Key="BrownBrush" Color="Brown"></SolidColorBrush>
    </Window.Resources>
    <Grid>
        <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>
    </Grid>
</Window>




MainWindow.xaml.cs

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

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

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


OutPut:





About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net 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

0 comments:

adsense

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