In this article I present a simple application with two buttons and two radio buttons. The idea is the following – each one of the two click buttons run a WAV file from the Windows ones. The radio buttons are used, to change the font of the click buttons.
What is interesting here – the colors are not explicitly written in the XAML code, but their values are defined in the code and are accessed as variables. The colors, defined in the XAML code may be also accessed from the C# code, with the “Find Resource” option:
1 |
c.Foreground = b.Foreground = FindResource("winbrush") as Brush; |
The above example is not exactly a good practice for coding, as far as a=b=c is not advisable from aesthetic point of view, but as far we now the way it runs (making “a” and “b” equal to “c”), it is OK for this example.
This is a screenshot of the application:
Below is the XAML code, used to build it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<Window x:Class="ResourceLogic.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Vitoshacademy" Height="150" Width="230" Background="{DynamicResource winbrush}"> <Window.Resources> <SolidColorBrush x:Key="winbrush">Yellow</SolidColorBrush> <SolidColorBrush x:Key="bgbrush">Green</SolidColorBrush> <SolidColorBrush x:Key="fgbrush">Red</SolidColorBrush> </Window.Resources> <StackPanel> <Button x:Name="b" Background="{StaticResource bgbrush}" Click ="Reminder" Foreground="{StaticResource fgbrush}" Width="120" Margin="5">Reminder</Button> <RadioButton IsChecked="False" Click="rb1_Click">Change font</RadioButton> <RadioButton Click="rb2_Click">Change it back</RadioButton> <Button x:Name="c" Background="{StaticResource bgbrush}" Click ="Guitar" Foreground="{StaticResource fgbrush}" Width="120" Margin="5">Guitar</Button> </StackPanel> </Window> |
Here comes the code from the C# part of the WPF:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System; using System.Collections.Generic; using System.Linq; using System.Media; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace ResourceLogic { /// <summary> /// The logic behind MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void rb1_Click(object sender, RoutedEventArgs e) { c.Foreground = b.Foreground = FindResource("fgbrush") as Brush; } private void rb2_Click(object sender, RoutedEventArgs e) { c.Foreground = b.Foreground = FindResource("winbrush") as Brush; } private void Reminder(object sender, RoutedEventArgs e) { SoundPlayer sp = new SoundPlayer("REMINDER.WAV"); sp.Play(); } private void Guitar(object sender, RoutedEventArgs e) { SoundPlayer sp = new SoundPlayer("Gitarrensound.WAV"); sp.Play(); } } } |
If you are willing to try this outstanding application ( 🙂 ), you may download it from here. If you are using Windows, you may receive the following message, while trying to download the application:
In stead of selecting “Report & Discard”, you should try with the “Keep” option in order to keep the application. The ZIP files contains the exe file + the two WAV files.
Enjoy it!