No-nonsense WPF Series 3: assigning values to object attributes in XAML

Source: Internet
Author: User

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.

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.