Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
22
Добавлен:
25.03.2016
Размер:
13.96 Mб
Скачать

Chapter 24 Performing Validation

495

This chapter has shown you how to perform basic validation by using the default exception validation rule processing provided by using data binding. You can also define your own custom validation rules if you want to perform more complex checks. For further information, see the documentation provided with Visual Studio 2008.

If you want to continue to the next chapter

Keep Visual Studio 2008 running, and turn to Chapter 25.

If you want to exit Visual Studio 2008 now

On the File menu, click Exit. If you see a Save dialog box, click Yes (if you are using Visual Studio 2008) or Save (if you are using Microsoft Visual C# 2008 Express Edition) and save the project.

Chapter 24 Quick Reference

To

Use data binding to bind a property of a control on a form to a property of an object

Do this

In the XAML code for the property of the control, specify a binding source identifying the object and the name of the property in the object to bind to. For example:

<TextBox ...>

<TextBox.Text>

<Binding Source=”{StaticResource customerData}” Path=”ForeName” />

</TextBox.Text>

</TextBox>

Enable a data binding to validate data entered by the user

Specify the Binding.ValidationRules element as part of the binding. For example:

<Binding Source=”{StaticResource customerData}” Path=”ForeName” /> <Binding.ValidationRules>

<ExceptionValidationRule/>

</Binding.ValidationRules>

</Binding>

496

Part IV Working with Windows Applications

 

 

Display error information in a

Define a style that detects a change to the Validation.HasError

nonintrusive manner

property of the control, and then set the ToolTip property of the

 

 

control to the message returned by the exception. Apply this style

 

 

to all controls that require validation. For example:

 

 

<Style x:Key=”errorStyle” TargetType=”Control”>

 

 

<Style.Triggers>

 

 

<Trigger Property=”Validation.HasError”

 

 

Value=”True”>

 

 

<Setter Property=”ToolTip”

 

 

Value=”{Binding RelativeSource=

 

 

{x:Static RelativeSource.Self},

 

 

Path=(Validation.Errors)[0].ErrorContent}” />

 

 

</Trigger>

 

 

</Style.Triggers>

 

 

</Style>

Validate all the controls on a

In the XAML code for the binding, set the UpdateSourceTrigger

form under programmatic control

property of the binding to “Explicit” to defer validation until the

rather than when the user moves

application requests it. To validate the data for all controls, create a

from control to control

BindingExpression object for each bound property of each control,

 

 

and call the UpdateSource method. Examine the HasError prop-

 

 

erty of each BindingExpression object. If this property is true, the

validation failed.

Microsoft Visual C# 2008 Step by Step

Part V

Managing Data

In this part:

Chapter 25. Querying Information in a Database . . . . . . . . . . . . . . . . . . . . . . . . . 499 Chapter 26. Displaying and Editing Data by Using Data Binding . . . . . . . . . . . . 529

497

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]