15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1006 ❘ ChaPTer 35 cOre wpf<br />

accessing resources from Code<br />

To access resources from code - behind, the base class FrameworkElement implements the method<br />

FindResource() , so you can invoke the FindResource() method with every WPF object.<br />

To do this, button1 doesn ’ t have a background specifi ed, but the Click event is assigned to the method<br />

button1_Click :<br />

< Button Name="button1" Width="220" Height="50" Margin="5" Click="button1_Click" ><br />

Apply Resource Programmatically<br />

< /Button ><br />

code snippet StylesAndResources/ResourceDemo.xaml<br />

With the implementation of button1_Click() , the FindResource() method is used on the Button that<br />

was clicked. Then a search for the resource MyGradientBrush happens hierarchically, <strong>and</strong> the brush is<br />

applied to the Background property of the control. The resource MyGradientBrush was created previously<br />

in the resources of the StackPanel :<br />

public void button1_Click(object sender, RoutedEventArgs e)<br />

{<br />

Control ctrl = sender as Control;<br />

ctrl.Background = ctrl.FindResource("MyGradientBrush") as Brush;<br />

}<br />

code snippet StylesAndResources/ResourceDemo.xaml .cs<br />

If FindResource() does not find the resource key, then an exception is thrown. If<br />

you don’t know for sure if the resource is available, then you can use the method<br />

TryFindResource() instead. TryFindResource() returns null if the resource is not<br />

found.<br />

dynamic resources<br />

With the StaticResource markup extension, resources are searched at load time. If the resource changes<br />

while the program is running, then you should use the DynamicResource markup extension instead.<br />

The next example is using the same resource as defi ned previously. The earlier example used<br />

StaticResource . This button uses DynamicResource with the DynamicResource markup extension. The<br />

event h<strong>and</strong>ler of this button changes the resource programmatically. The h<strong>and</strong>ler method button2_Click is<br />

assigned to the Click event h<strong>and</strong>ler:<br />

< Button Name="button2" Width="200" Height="50" Foreground="White" Margin="5"<br />

Background="{DynamicResource MyGradientBrush}" Content="Change Resource"<br />

Click="button2_Click" / ><br />

code snippet StylesAndResources/ResourceDemo.xaml<br />

The implementation of button2_Click() clears the resources of the StackPanel <strong>and</strong> adds a new resource<br />

with the same name, MyGradientBrush . This new resource is very similar to the resource that is defi ned in<br />

XAML code; it just defi nes different colors:<br />

private void button2_Click(object sender, RoutedEventArgs e)<br />

{<br />

myContainer.Resources.Clear();<br />

var brush = new LinearGradientBrush<br />

{<br />

StartPoint = new Point(0, 0),<br />

EndPoint = new Point(0, 1)<br />

};<br />

brush.GradientStops = new GradientStopCollection()<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!