In Silverlight, you can use "binding =" {binding name} "to bind data with the control in XAML and format it as needed; using this method, you can easily bind collection objects, XML files, WCF services, data tables, and custom objects in SL;
During normal development, we may encounter a value (0, 1) or a Boolean (true or false; when we finally display the information we really want to show as (male, female) or (borrow, loan), the demo below will be useful to you;
1. First introduce binding. the converter attribute is mainly used to call a custom converter when binding data to convert incompatible data types into the data types we need, which is equivalent to setting up two bridges for conversion of different data types. Official explanation: gets or sets the converter object. when data is transmitted between the source and the target (or in the opposite direction), the binding engine calls this object to modify the data. Reference: http://msdn.microsoft.com/zh-cn/library/system.windows.data.binding.converter (vs.95). aspx
2. final effect of the demo:
3. Implementation steps
3.1 custom type converter (implementation of the ivalueconverter interface convert and convertback methods) such:
Public class sexconverter: ivalueconverter <br/>{< br/> Public object convert (object value, type targettype, object parameter, system. globalization. cultureinfo culture) <br/>{< br/> If (targettype! = Typeof (string) throw new invalidoperationexception ("the target must be a integer! "); <Br/> return (INT) value) = 0? "Female": "male"); <br/>}</P> <p> Public object convertback (object value, type targettype, object parameter, system. globalization. cultureinfo culture) <br/>{< br/> If (targettype! = Typeof (int32) throw new invalidoperationexception ("the target must be a string! "); <Br/> return (value. tostring () =" female "? 0: 1); <br/>}</P> <p>
3.2 introduce the type converter in application. Resources, for example:
<Uconvert: sexconverter X: Key = "sexconvert"/> <br/>
3.3 apply a forwarder in data binding, for example:
<Data: Maid header = "gender" binding = "{binding sex, converter = {staticresource sexconvert}, mode = twoway}"/> <br/>
4. Complete body code:
4.1 app. XAML file code:
<Application xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" <br/> xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" <br/> X: class = "loadsilverlight. APP "<br/> xmlns: uconvert =" CLR-namespace: loadsilverlight "<br/> <application. resources> <br/> <uconvert: sexconverter X: Key = "sexconvert"/> <br/> <uconvert: loanconverter X: key = "loanconvert"/> <br/> </application. resources> <br/> </Application> <br/>
4.2 All CS codes:
Namespace loadsilverlight <br/>{< br/> Public partial class uconverterdemo: usercontrol <br/>{< br/> Public uconverterdemo () <br/>{< br/> initializecomponent (); <br/> This. loaded + = new routedeventhandler (uconverterdemo_loaded); <br/> List <dataitem> datas = new list <dataitem> (); <br/> datas. add (New dataitem {sex = 0, loan = true, name = "Jack"}); <br/> datas. add (New dataitem {sex = 1, loan = True, name = "Lily"}); <br/> datas. add (New dataitem {sex = 0, loan = false, name = "Jessica"}); </P> <p> This. dataGrid. itemssource = datas; <br/>}</P> <p> void uconverterdemo_loaded (Object sender, routedeventargs E) <br/>{</P> <p >}< br/>}</P> <p> # region custom value converter <br/> public class sexconverter: ivalueconverter <br/>{< br/> Public object convert (object value, type targettype, object parameter, System. Globalization. cultureinfo culture) <br/>{< br/> If (targettype! = Typeof (string) throw new invalidoperationexception ("the target must be a integer! "); <Br/> return (INT) value) = 0? "Female": "male"); <br/>}</P> <p> Public object convertback (object value, type targettype, object parameter, system. globalization. cultureinfo culture) <br/>{< br/> If (targettype! = Typeof (int32) throw new invalidoperationexception ("the target must be a string! "); <Br/> return (value. tostring () =" female "? 0: 1); <br/>}</P> <p> public class loanconverter: ivalueconverter <br/>{< br/> Public object convert (object value, type targettype, object parameter, system. globalization. cultureinfo culture) <br/>{< br/> If (targettype! = Typeof (object) throw new invalidoperationexception ("the target must be a string! "); <Br/> return (bool) value) = true? "Borrow": "loan"); <br/>}< br/> Public object convertback (object value, type targettype, object parameter, system. globalization. cultureinfo culture) <br/>{< br/> If (targettype! = Typeof (Boolean) throw new invalidoperationexception ("the target must be a string! "); <Br/> return (value. tostring () =" borrow "? True: false ); <br/>}< br/> # endregion </P> <p> // <summary> <br/> // custom data entity <br/> // </Summary> <br/> public class dataitem <br/> {<br/> private int _ sex; <br/> private bool _ loan; <br/> Public int sex <br/>{< br/> get {return _ sex ;} <br/> set <br/>{< br/> _ sex = value; <br/> onsexchanged ("sex "); <br/>}< br/> Public bool loan <br/>{< br/> get {return _ loan ;}< br/> set <BR/>{< br/> _ loan = value; <br/> onsexchanged ("loan "); <br/>}< br/> Public string name {Get; Set ;}< br/> public event propertychangedeventhandler sexchanged; <br/> public event propertychangedeventhandler loanchanged; </P> <p> // event called when the attribute is changed during bidirectional binding <br/> protected void onsexchanged (string sex) <br/>{< br/> If (sexchanged! = NULL) <br/>{< br/> sexchanged (this, new propertychangedeventargs (sex )); <br/>}< br/> protected void onloanchanged (string loan) <br/>{< br/> loanchanged (this, new propertychangedeventargs (loan); <br/>}</P> <p>
4.3 complete body code of the XAML page:
<Usercontrol X: class = "loadsilverlight. uconverterdemo "<br/> xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "<br/> xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "<br/> xmlns: Data =" CLR-namespace: system. windows. controls; Assembly = system. windows. controls. data "<br/> width =" 400 "Height =" 300 "> <br/> <usercontrol. resources> <br/> <! -- You can also reference the custom converter here. We recommend that you use the global app. XAML reference --> <br/> </usercontrol. resources> <br/> <grid X: Name = "layoutroot" background = "white"> <br/> <data: DataGrid grid. row = "0" X: Name = "DataGrid" margin = "3,3, 3,3" autogeneratecolumns = "false"> <br/> <data: DataGrid. columns> <br/> <data: datagridtextcolumn header = "name" binding = "{binding name}" isreadonly = "true"/> <br/> <data: datagridtextcolumn header = "gender" binding = "{binding sex, converter = {staticresource sexconvert}, mode = twoway}"/> <br/> <data: datagridtemplatecolumn header = "loan situation"> <br/> <data: datagridtemplatecolumn. celltemplate> <br/> <datatemplate> <br/> <checkbox ischecked = "{binding loan, mode = twoway}" content = "{binding loan, converter = {staticresource loanconvert} "/> <br/> </datatemplate> <br/> </data: datagridtemplatecolumn. celltemplate> </P> <p> </data: datagridtemplatecolumn> <br/> </data: DataGrid. columns> <br/> </data: DataGrid> <br/> </GRID> <br/> </usercontrol> </P> <p>
5. This article ends