WPF Learning Path (ii) XAML (cont.)

Source: Internet
Author: User

Property

Simple properties

The previously used width/height are simple attributes, and their assignments must be placed in double quotes.

The XAML parser performs an implicit conversion based on the type of the property

The difference from C #

Solidbrush.color = Colors.aliceblue;

<button Color = "AliceBlue"/>

Some enum types in C # can be combined with (|) operators, separated by (,) in XAML

Attached properties

Attached properties can be used with multiple controls, but properties defined in another class are often used in WPF for layout

such as dockpanel.dock= "left" can also use the following form

<DockPanel.Dock>Left</DockPanel.Dock>

Content Properties

There are several ways to do this

<button content= "Hello WPF"/>

<button><button.content>hello wpf</button.content></button>

<button>hello wpf</button>

No additional information is available in the content,except for the TextBlock content , which can be placed in bold and italic labels

<TextBlock>
<italic>hello,</italic><bold>xaml</bold>
</TextBlock>

Custom controls can also add content properties

[Contentproperty ("Text")]
public class Book {public string Text {get; set;}//:}

<local:book name= "self-learning WPF" price= "30.0" >hello wpf</local:book>

Type converters

A string in XAML becomes a CLR object through a type converter

Custom type Converters

<local:book name= "self-learning WPF" price= "$ $"/>

public class Book
{

Public Moneytype price {get; set;}

}

[TypeConverter (typeof (MoneyConverter))]
public class Moneytype
{
private double value;
Public Moneytype () {this.value = 0;}
Public Moneytype (double value) {this.value = value;}
public override string ToString () {return this.value.ToString ();}
public static Moneytype Parse (string value)
{
String str = value. Trim ();
if (str[0] = = ' $ ')
{
string newvalue = str. Remove (0, 1);
Double realvalue = Double. Parse (NewValue);
return new Moneytype (Realvalue * 8);
}
Else
{
Double realvalue = Double. Parse (value);
return new Moneytype (Realvalue);
}
}
}

public class Moneyconverter:typeconverter
{
public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourcetype)
{
if (sourcetype = = typeof (String))
return true;
Return base. CanConvertFrom (context, sourcetype);
}

public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationtype)
{
if (destinationtype = = typeof (String))
return true;
Return base. CanConvertTo (context, destinationtype);
}

public override Object ConvertFrom (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object Value
{
if (value. GetType ()! = typeof (String))
Return base. ConvertFrom (context, culture, value);
Return Moneytype.parse ((string) value);
}

public override Object ConvertTo (ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object Value, Type destinationtype)
{
if (destinationtype = = typeof (String))
Return base. ConvertTo (context, culture, value, destinationtype);
return value. ToString ();
}
}

Canconvertfrom\canconvertto\convertfrom\convertto these four methods need to be rewritten

Markup extension

XAML is not supported in the following scenarios

1. Assigning a property to null

2. Assigning a property to an already static variable

Unlike implicit conversions of type converters, markup extensions are implemented by displaying syntax calls

In XAML, as long as the property is enclosed in {}, it is considered a markup extension.

Assign a property to a value of Null {x:null}

<button name= "Btn01" content= "button" click= "Btn01_click_1" background= "{x:null}"/>

Assigning a property to an already static variable

<button name= "Btn01" content= "button" click= "Btn01_click_1" >
<Button.Background>
<x:static member= "Systemcolors.activecaptionbrush"/>
</Button.Background>
</Button>

If there is {} in the string you want to display, add a {} before the string

<textblock text= "{}{hello XAML}"/>

XAML can not only be applied to WPF

To be continue ...

WPF Learning Path (ii) XAML (cont.)

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.