Void type and custom converter in Silverlight Data Binding

Source: Internet
Author: User

In Silverlight, You can bind the bound destination attribute to connect to the data source. For example:

XAMLCode:

< Usercontrol X: Class = "Validatordemo. bindingsample"
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Width = "400" Height = "300" >
< Stackpanel X: Name = "Layoutroot" Width = "200" >
< Textbox Text =" {Binding name, mode = twoway} " Margin = "10"   > </ Textbox >
< Textbox Text =" {Binding age, mode = twoway} " Margin = "10" > </ Textbox >
< Button Content = "Show" Click = "Show" Margin = "10" > </ Button >
</ Stackpanel >
</ Usercontrol >

C # code:

Public   Partial   Class Bindingsample: usercontrol
{
Public Bindingsample ()
{
Initializecomponent ();

This. Layoutroot. datacontext= This. People;
}

PrivatePeople people= NewPeople ();

private void show ( Object sender, routedeventargs e)
{< br> MessageBox. show ( string . format ( " {0} is {1 }. " , This . people. name, This . people. age);
}< BR >}

Public ClassPeople
{
Public StringName {Get;Set;}
Public Int?Age {Get;Set;}
}

RunProgram, Such:

 

At this time, if you clear the value in the second text box (show age) and click the "show" button, the problem occurs: attribute in the data source (here is the age attribute in People) the value is not changed to null, but to 8 before modification.

If you assign the value of the bound validatesonexceptions attribute to true, you can see a clearer result:

 

The second text box is marked with a clear sign, indicating that the input format is incorrect. This indicates that the binding engine caught an exception when the binding target updates the source object. Further, it can be understood that the default converter cannot process the null string to the int? Type conversion. To solve this problem, we can customize the following converter:

Public   Class Intconverter: ivalueconverter
{
Public   Object Convert ( Object Value, type targettype, Object Parameter, cultureinfo Culture)
{
Return Value =   Null   ?   Null : Value. tostring ();
}
Public   Object Convertback ( Object Value, type targettype, Object Parameter, cultureinfo Culture)
{
String Str = Value As   String ;
If ( String . Isnullorempty (STR )) Return   Null ;
Str = Str. Trim ();
If (Str. Length =   0 ) Return   Null ;
Int V;
Bool Isvalid =   Int . Tryparse (STR, Out V );
If ( ! Isvalid) Return   Null ;
Return V;
}
}

The XAML code for using a custom forwarder is as follows:

< Usercontrol X: Class = "Validatordemo. bindingsample"
Xmlns = "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: Local = "CLR-namespace: validatordemo"
Width = "400" Height = "300" >
< Stackpanel X: Name = "Layoutroot" Width = "200" >
< Stackpanel. Resources >
< Local: intconverter X: Key = "Intconverter" > </ Local: intconverter >
</ Stackpanel. Resources >
< Textbox Text =" {Binding name, mode = twoway, validatesonexceptions = true} " Margin = "10"   > </ Textbox >
< Textbox Text =" {Binding age, mode = twoway, validatesonexceptions = true, converter = {staticresource intconverter }} "
Margin = "10" > </ Textbox >
< Button Content = "Show" Click = "Show" Margin = "10" > </ Button >
</ Stackpanel >
</ Usercontrol >

 So far, from an empty string to an int? Solved the problem of Type null value conversion. However, the following two lines of code in the Custom converter still have problems:

Bool Isvalid =   Int . Tryparse (STR, Out V );
If ( ! Isvalid) Return   Null ;

From string to int? During type conversion, if the string is not a valid numeric string, the conversion will not succeed. If null is returned, the converter will not throw an exception, and the binding engine will certainly not be able to catch exceptions in the conversion process. If an exception is thrown during the conversion process, unfortunately:

 

The Data Binding engine does not capture exceptions caused by user-provided converters. Any exceptions caused by the convertback method or any uncaptured exceptions caused by the method called by the convertback method are considered as runtime errors. Return dependencyproperty. unsetvalue to handle the expected problem. (Msdn original)

 

How can we solve this problem perfectly? Hope you can give some advice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.