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.

Data Binding ❘ 1051<br />

< Label ><br />

< Label.Content ><br />

< PriorityBinding ><br />

< Binding Path="ProcessSomeData" Source="{StaticResource data1}"<br />

IsAsync="True" / ><br />

< Binding Path="Info2" Source="{StaticResource info}"<br />

IsAsync="True" / ><br />

< Binding Path="Info1" Source="{StaticResource info}"<br />

IsAsync="False" / ><br />

< /PriorityBinding ><br />

< /Label.Content ><br />

< /Label ><br />

With the start of the application you can see the message please wait … in the user interface. After a few<br />

seconds the result from the Info2 property is returned as please wait a little more . It replaces the<br />

output from Info1 . Finally, the result from ProcessSomeData replaces the output again.<br />

Value Conversion<br />

Let ’ s get back to the BooksDemo application. The authors of the book are still missing in the user interface.<br />

If you bind the Authors property to a Label element, the ToString() method of the Array class is<br />

invoked, which returns the name of the type. One solution to this is to bind the Authors property to a<br />

ListBox . For the ListBox , you can defi ne a template for a specifi c view. Another solution is to convert the<br />

string array returned by the Authors property to a string <strong>and</strong> use the string for binding.<br />

The class StringArrayConverter converts a string array to a string. WPF converter classes must<br />

implement the interface IValueConverter from the namespace System.Windows.Data . This interface<br />

defi nes the methods Convert() <strong>and</strong> ConvertBack() . With the StringArrayConverter , the Convert()<br />

method converts the string array from the variable value to a string by using the String.Join() method.<br />

The separator parameter of the Join() is taken from the variable parameter received with the<br />

Convert() method.<br />

You can read more about the methods of the String classes in Chapter 9, “ Strings <strong>and</strong><br />

Regular Expressions.”<br />

using System;<br />

using System.Diagnostics.Contracts;<br />

using System.Globalization;<br />

using System.Windows.Data;<br />

namespace Wrox.ProCSharp.WPF.Utilities<br />

{<br />

[ValueConversion(typeof(string[]), typeof(string))]<br />

class StringArrayConverter : IValueConverter<br />

{<br />

public object Convert(object value, Type targetType, object parameter,<br />

CultureInfo culture)<br />

{<br />

Contract.Requires(value is string[]);<br />

Contract.Requires(parameter is string);<br />

string[] stringCollection = (string[])value;<br />

string separator = (string)parameter;<br />

}<br />

return String.Join(separator, stringCollection);<br />

public object ConvertBack(object value, Type targetType, object parameter,<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!