Free Exam Prep: 70-511: Using the SetResourceReference


 

The SetResourceReference is one of the three ways you can use Resources in Windows Presentation Foundation.

Link:

I have included my code attached to this blog and a video below.

The form generated from the XAML Looks like this, each button changes the color of the other button and modifies the message in the textblock.

image Form with it first runs

image Form when you click the button with content or text: “Who?”

image Form when you click the button with content or text: “What?”

XAML (Default: MainWindow.XAML)

<Window x:Class="SetResourceReference.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="SetResourceReference" Height="125" Width="200"> <Window.Resources> <SolidColorBrush x:Key="brushRed" Color="Red" /> <SolidColorBrush x:Key="brushBlue" Color="Blue" /> <SolidColorBrush x:Key="brushYellow" Color="Yellow" /> </Window.Resources> <Canvas Background="PeachPuff"> <Button x:Name="btn" Click="btn_Click" Height="50" Content="Who?" Margin="25,50,0,50" Foreground="FloralWhite" Background="#FF494949" ToolTip="What the heck click it" Canvas.Left="-25" Canvas.Top="-14" Width="70" /> <Button Canvas.Left="103" Canvas.Top="36" Content="What?" Height="50" Name="button1" Click="button1_Click" Width="75" /> <TextBlock x:Name="txtblk"> Click one of the buttons </TextBlock> </Canvas> </Window>

Code behind the XAML Page (Default: MainWindow.XAML.cs, note: unused using statements were removed for brevity):

using System.Windows; namespace SetResourceReference { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void btn_Click(object sender, RoutedEventArgs e) { this.txtblk.Text = "Martians"; /* Assign a resource reference by using SetResourceReference method * The SetResourceReference takes 2 parameters. * The first parameter is the dependency property * The second parameter is the x:Key name from the XAML page */ /****object **********************Dependency Property, x:Key */ this.button1.SetResourceReference(BackgroundProperty, "brushRed"); } private void button1_Click(object sender, RoutedEventArgs e) { this.txtblk.Text = "Mars"; /* Assign a resource reference by using SetResourceReference method * The SetResourceReference takes 2 parameters. * First parameter is the dependency property * Second parameter is the x:Key name from the XAML page */ /****object ******************Dependency Property, x:Key */ this.btn.SetResourceReference(BackgroundProperty, "brushBlue"); } } }

 

SetResourceReference.zip

Skip to main content