1. assign values to object attributes using the attribute of tags
We can assign values using strings in the above method, but for example, if the above fill is actually of the shape. Fill type, why can I assign red? This is because WPF uses typeconverter for conversion.
2. Use the typeconverter class to convert the attribute of XAML to the property of the object.
Namespace deepxaml {[typeconverter (typeof (stringtopersonconverter)] public class person {public string name {Get; Set ;}public person child {Get; Set ;}}}
Namespace deepxaml {public class stringtopersonconverter: typeconverter {public override object convertfrom (itypedescriptorcontext context, system. globalization. cultureinfo culture, object Value) {If (value is string) {person P = new person (); p. name = value as string; return P;} return base. convertfrom (context, culture, value );}}}
Public partial class mainwindow: window {public mainwindow () {initializecomponent ();} private void button_click (Object sender, routedeventargs e) {person P = (person) This. findresource ("Jack"); MessageBox. show (P. child. name );}}
3. Markup extensions)
Tag extension is a special attribute = Value Syntax. value is composed of a pair of curly braces and their enclosed content. The XAML compiler parses such content to generate corresponding objects.
<Grid> <stackpanel> <button X: name = "btnok" width = "200" Height = "100" Click = "button_click"> OK </button> <textbox text = "{binding elementname = btnok, path = width, mode = oneway} "width =" 200 "margin =" 10 "> </textbox> </stackpanel> </GRID>
Tag extension can be nested with some abbreviations, such as {binding value ,...} And {binding Path = value ,...} It is equivalent. The former is a fixed-position parameter, and the latter specifies the parameter name. The location order can be changed. The class name that marks the extension class is suffixed with extension, but this suffix can be omitted in XAML.
4. ImportProgramSet and reference namespace
The image above can be demonstrated.