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.

Data Binding ❘ 1049<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

FirstLast<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

code snippet MultiBindingDemo/MainWindow.xaml<br />

The multi-value converter implements the interface IMultiValueConverter. This interface defines two<br />

methods, Convert() <strong>and</strong> ConvertBack(). Convert() receives multiple values with the first argument from<br />

the data source <strong>and</strong> returns one value to the target. With the implementation, depending on whether the<br />

parameter has a value of FirstLast or LastFirst, the result is created differently:<br />

using System;<br />

using System.Globalization;<br />

using System.Windows.Data;<br />

namespace Wrox.ProCSharp.WPF<br />

{<br />

public class PersonNameConverter : IMultiValueConverter<br />

{<br />

public object Convert(object[] values, Type targetType, object parameter,<br />

CultureInfo culture)<br />

{<br />

switch (parameter as string)<br />

{<br />

case "FirstLast":<br />

return values[0] + " " + values[1];<br />

case "LastFirst":<br />

return values[1] + ", " + values[0];<br />

default:<br />

throw new ArgumentException(<br />

String.Format("invalid argument {0}", parameter));<br />

}<br />

}<br />

}<br />

}<br />

public object[] ConvertBack(object value, Type[] targetTypes,<br />

object parameter, CultureInfo culture)<br />

{<br />

throw new NotSupportedException();<br />

}<br />

code snippet MultiBindingDemo/MainWindow.xaml.cs<br />

Priority binding<br />

PriorityBinding makes it easy to bind to data that is not readily available. If you need time to get the<br />

result with PriorityBinding, you can inform the user about the progress so he knows to wait.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!