Template_Wpf ( WPF )

Objectives:
  • Using ResourceDictionary 
  • Dictionary1.xaml
    • <SolidColorBrush x:Key="myBrush1" Color="Azure"/>
Step:1 Create a New project

Add  Dictionary1.xaml using Resource Dictionary menu

Dictionary.xaml does not have Code-Behind file and Design Pane

Save the application

Step:2 Edit Code in Dictionary.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="myBrush1" Color="Azure"/>

</ResourceDictionary>

Step:3 Edit Code in MainWindow.xaml.cs

Code MainWindow.xaml

<Window x:Class="WpfResourcesApp3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="248" Width="261">
<Window.Resources>
<ResourceDictionary Source="Dictionary1.xaml"/>
</Window.Resources>
<Grid>
<Button Content="Button" Height="42" HorizontalAlignment="Left"
Margin="52,48,0,0" Name="button1"
VerticalAlignment="Top" Width="88" />
<Button Content="Button" Height="32" HorizontalAlignment="Left"
Margin="52,104,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>

 

Code MainWindow.xaml.cs

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

namespace WpfResourcesApp3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
button1.Background = (Brush)FindResource("myBrush1");
button2.SetResourceReference(BackgroundProperty, "myBrush2");
}
}
}
 

Step:4 Runtime view