15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

736 ❘ ChaPTer 27 COre xAml<br />

public static readonly DependencyProperty ValueProperty =<br />

DependencyProperty.Register("Value", typeof(int), typeof(MyDependencyObject),<br />

new PropertyMetadata(0, OnValueChanged, CoerceValue));<br />

//...<br />

}<br />

}<br />

private static void OnValueChanged(DependencyObject obj,<br />

DependencyPropertyChangedEventArgs args)<br />

{<br />

int oldValue = (int)args.OldValue;<br />

int newValue = (int)args.NewValue;<br />

//...<br />

}<br />

code snippet DependencyObjectDemo/MyDependencyObject.cs<br />

bubbling <strong>and</strong> Tunneling eVenTs<br />

Elements can be contained in elements. With XAML <strong>and</strong> WPF, you can defi ne that a Button contains a<br />

ListBox ; the ListBox contains items that are Button controls again. When you click on an inner control,<br />

the event should go all the way to the outside. WPF supports bubbling <strong>and</strong> tunneling events. Often these<br />

events are used in pairs. The PreviewMouseMove event is a tunneling event that tunnels from the outside to<br />

the inside. MouseMove follows the PreviewMouseMove event <strong>and</strong> is a bubbling event that bubbles from the<br />

inside to the outside.<br />

Core information about .<strong>NET</strong> events is explained in Chapter 8, “ Delegates, Lambdas,<br />

<strong>and</strong> Events.”<br />

To demonstrate bubbling, the following XAML code contains four Button controls where the surrounding<br />

StackPanel defi nes an event h<strong>and</strong>ler for the Button.Click event named OnOuterButtonClick() .<br />

button2 contains a ListBox that has two Button controls as its children <strong>and</strong> the Click event h<strong>and</strong>ler<br />

OnButton2() . Both of the inner buttons also have an event h<strong>and</strong>ler associated with the Click event.<br />

< Window x:Class=" Wrox.ProCSharp.XAML.MainWindow"<br />

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br />

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br />

Title="MainWindow" Height="350" Width="525" ><br />

< StackPanel x:Name="stackPanel1" Button.Click="OnOuterButtonClick" ><br />

< Button x:Name="button1" Content="Button 1" Margin="5" / ><br />

< Button x:Name="button2" Margin="5" Click="OnButton2" ><br />

< ListBox x:Name="listBox1" ><br />

< Button x:Name="innerButton1" Content="Inner Button 1" Margin="4"<br />

Padding="4" Click="OnInner1" / ><br />

< Button x:Name="innerButton2" Content="Inner Button 2" Margin="4"<br />

Padding="4" Click="OnInner2" / ><br />

< /ListBox ><br />

< /Button ><br />

< ListBox ItemsSource="{Binding}" / ><br />

< /StackPanel ><br />

< /Window ><br />

code snippet BubbleDemo/MainWindow.xaml<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!