WPF学习之路(二) XAML(续)

属性

简单属性

前面用到的Width/Height都是简单属性,其赋值一定要放到双引号里

XAML解析器会根据属性的类型执行隐式转换

与C#的区别

SolidBrush.Color = Colors.AliceBlue;

<Button Color = "AliceBlue" />

C#中有的枚举类型可以用(|)运算符组合,在XAML中用(,)分隔

附加属性

 附加属性可以用于多个控件,但是在另外一个类中定义的属性,在WPF中常常用于布局

如DockPanel.Dock="Left" 也可以使用下面的形式

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

Content属性

有以下几种写法

<Button Content="Hello WPF" />

<Button><Button.Content>Hello WPF</Button.Content></Button>

<Button>Hello WPF</Button>

Content中不能有其他信息,TextBlock的Content例外,可以放置加粗和斜体标签

<TextBlock>
  <Italic>Hello ,</Italic><Bold>XAML</Bold>
</TextBlock>

自定义控件也可以添加Content属性

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

<local:Book Name="Self-learning WPF" Price="30.0">Hello WPF</local:Book>

类型转换器

XAML中的字符串通过类型转换器变成CLR对象

自定义类型转换器

<local:Book Name="Self-learning WPF" Price="$5" />

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();
  }
}

CanConvertFromCanConvertToConvertFromConvertTo这四个方法需要重写

标记扩展

 XAML有以下情况无法支持

1.将属性赋值为null

2.将属性赋值给已经静态变量

不同于类型转换器的隐式转换,标记扩展通过显示的语法调用实现

在XAML中只要属性被{}括起来,会被认为是一个标记扩展。

将属性赋值为null {x:Null}

 <Button Name="btn01" Content="Button" Click="btn01_Click_1" Background="{x:Null}" />

将属性赋值给已经静态变量

<Button Name="btn01" Content="Button" Click="btn01_Click_1">
  <Button.Background>
    <x:Static Member="SystemColors.ActiveCaptionBrush" />
  </Button.Background>
</Button>

如果想显示的字符串中有{} ,在字符串前添加一个{}

 <TextBlock Text="{}{Hello XAML}" />

XAML不止可以应用于WPF

To be continue...

原文地址:https://www.cnblogs.com/alex09/p/4428408.html