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 ❘ 1057<br />

< Label Style="{StaticResource labelStyle}"<br />

Content="{Binding XPath=Publisher}"<br />

Grid.Row="1" Grid.Column="1" / ><br />

< Label Style="{StaticResource labelStyle}"<br />

Content="{Binding XPath=@isbn}"<br />

Grid.Row="2" Grid.Column="1" / ><br />

< /Grid ><br />

< /Window ><br />

code snippet BooksDemo/MainWindow.xaml<br />

Figure 36 - 10 shows the result of the XML binding.<br />

figure 36-10<br />

If XML data should be shown hierarchically, you can use the TreeView control.<br />

binding Validation<br />

Several options are available to validate data from the user before it is used with the .<strong>NET</strong> objects. These<br />

options are:<br />

➤<br />

➤<br />

➤<br />

H<strong>and</strong>ling exceptions<br />

Data error information<br />

H<strong>and</strong>ling exceptions<br />

Custom validation rules<br />

One of the options demonstrated here is that the .<strong>NET</strong> class throws an exception if an invalid value is set as<br />

shown in the class SomeData . The property Value1 accepts only values larger or equal to 5 <strong>and</strong> smaller than 12:<br />

public class SomeData<br />

{<br />

private int value1;<br />

public int Value1 {<br />

get<br />

{<br />

return value1;<br />

}<br />

set<br />

{<br />

if (value < 5 || value > 12)<br />

throw new ArgumentException(<br />

"value must not be less than 5 or greater than 12");<br />

value1 = value;<br />

}<br />

}<br />

}<br />

code snippet ValidationDemo/SomeData.cs<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!