Attribute 与 Property 之间的区别

  • Property 对应着抽象对象身上的性状;
  • Attribute 是针对标签的特征;
  • 往往一个标签具有的 Attribute 对于它所代表的对象的 Property。

1. 为对象属性赋值

1.1 使用标签的 Attribute 为对象属性赋值;

<Rectangle x:Name="rectangle" Width="200" Height="200" Fill="Blue"/>

其中对 Fill 的赋值等价于后台代码:

SolidColorBrush sBrush = new SolidColorBrush();
sBrush.Color = Colors.Blue;
this.rectangle.Fill = sBrush;

1.2 使用 TypeConverter 将 XAML 上的 Attribute 与对象的 Property 进行映射

public class Human
{
public string Name { get; set; }
public Human Child { get; set; }
} <Window.Resources>
<local:Human x:Key="human" Name="maomao" Child="ABC" />
</Window.Resources>
<Grid>
<Button Content="click me" Click="Button_Click"/>
</Grid> private void Button_Click(object sender, RoutedEventArgs e)
{
Human hu = (Human)this.FindResource("human");
MessageBox.Show(hu.Child.Name);
}

会抛出 ArgumentException 异常:System.String 的对象无法转换成类;

解决方式:

[TypeConverter(typeof(StringToHumanTypeConverter))]
public class Human
{
public string Name { get; set; }
public Human Child { get; set; }
} public class StringToHumanTypeConverter : System.ComponentModel.TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
Human h = new Human();
h.Name = value as string;
return h;
}
return base.ConvertFrom(context, culture, value);
}
}

1.3 属性元素

<Rectangle>
<Rectangle.Fill>
<SolidColorBrush Color="Blue" />
</Rectangle.Fill>
</Rectangle>

在需要的时候运用,切勿过犹不及。一般属性的值是复杂对象时用。

<Rectangle>
<Rectangle.Fill>
<LinearGradientBrush>
<GradientStop Color="AliceBlue" Offset="0.2"/>
<GradientStop Color="Blue" Offset="0.7"/>
<GradientStop Color="DarkBlue" Offset="1.0"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>

1.4 标记拓展

<TextBox Text="{Binding ElementName=slider1, Path=Value, Mode=OneWay}"/>

还可以嵌套

<TextBox Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}"/>
  • 一些缩写方法:(具体的原因这个后面再讲)
  • {Binding ElementName=slider1, Path=Value} = {Binding Value, ElementName=slider1}
  • {StaticResource xx,...} = {StaticResource ResourceKey=xx,...}
  • Text="{x:Static ...}" = Text="{x:StaticExtension ...}"
<Window.Resources>
<local:Human x:Key="human" Name="maomao" Child="ABC" />
</Window.Resources>
<TextBox Text="{Binding Source={StaticResource ResourceKey=human}, Path=Name}"/>
<TextBox Text="{x:Static local:Hei.man}"/> public class Hei
{
public static string man = "What is up";
}

2. 代码后置

xx.xaml.cs 只是为了方便管理文件,非必须,xaml 解析器会找 x:Class 对应的类;

可以使用 <x:Code> 把代码后置的 C# 代码搬到 xaml 中。

<Button  Grid.Row="3" Content="click me" Click="Button_Click"/>
<x:Code>
<![CDATA[
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("I am from X:Code");
}
]]>
</x:Code>

3. 导入程序集并引用其中的命名中间

  1. 编写库类项目并编译得到 .dll 文件或者获得别人的 .dll 文件;
  2. 将类库项目或者 .dll 引用进自己的项目;
  3. 在 C# 和 xaml 中引用类库中的名称空间。
<Window x:Class="WpfApp1.CommandMode.CommandModeWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1.CommandMode"
xmlns:Control="clr-namespace:Controls;assembly=MyLibrary"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Window.Resources>
<sys:String x:Key="stringA">This is a </sys:String>
</Window.Resources>
...
</Window>
  • xmlns 是 XMAL 中用于声明命名空间的 Attribute。
  • xmlns:映射名="clr-namespace:库类中的命名空间名称;assembly=库类文件名"
  • 引用语法:<映射名:类名>,又如:<Controls:MessagePanel x:Name="window1">

WPF 基础 - xaml 语法总结的更多相关文章

  1. 4 WPF学习---系统的学习XAML语法

    转载:http://blog.csdn.net/fwj380891124/article/details/8093001 1,XAML文档的树形结构: UI在用户眼里面是个平面结构.如下图所示,在用户 ...

  2. XAML属性赋值转换之谜(WPF XAML语法解密)

    XAML与XML类似,就是XML延伸过来的.为了更好的表达一些功能,WPF对XML做了扩展,有些功能是WPF在后台悄悄的替你做了.有时候,虽然实现了某个功能,但是对实现原理还是很茫然.今天就讲讲XAM ...

  3. WPF中 PropertyPath XAML 语法

    原文:WPF中 PropertyPath XAML 语法 PropertyPath 对象支持复杂的内联XAML语法用来设置各种各样的属性,这些属性把PropertyPath类型作为它们的值.这篇文章讨 ...

  4. WPF笔记(1.1 WPF基础)——Hello,WPF!

    原文:WPF笔记(1.1 WPF基础)--Hello,WPF! Example 1-1. Minimal C# WPF application// MyApp.csusing System;using ...

  5. .Net Core WPF之XAML概述

    原文链接,机器翻译,有误处参看原文. XAML overview in WPF 2019/08/08 What is XAML XAML syntax in brief Case and white ...

  6. WPF基础到企业应用系列6——布局全接触

    本文转自:http://knightswarrior.blog.51cto.com/1792698/365351 一. 摘要 首先很高兴这个系列能得到大家的关注和支持,这段时间一直在研究Windows ...

  7. 基本 XAML 语法指南

    我们介绍了 XAML 语法规则,以及用于描述 XAML 语法中存在的限制或选项的术语.当出现以下情况时你会发现本主题很有用:不熟悉 XAML 语言的使用,希望加强对术语或某些语法部分的理解,或者对 X ...

  8. WPF:XAML概述

    简介 XAML是eXtensible Application Markup Language可扩展应用程序标记语言,它是微软公司为构建应用程序用户界面而创建的一种新的描述性语言.XAML提供了一种便于 ...

  9. wpf中xaml的类型转换器与标记扩展

    原文:wpf中xaml的类型转换器与标记扩展 这篇来讲wpf控件属性的类型转换器 类型转换器 类型转换器在asp.net控件中已经有使用过了,由于wpf的界面是可以由xaml组成的,所以标签的便利也需 ...

随机推荐

  1. 设计模式(二十三)——策略模式(Arrays源码分析)

    1 编写鸭子项目,具体要求如下: 1) 有各种鸭子(比如 野鸭.北京鸭.水鸭等, 鸭子有各种行为,比如 叫.飞行等) 2) 显示鸭子的信息 2 传统方案解决鸭子问题的分析和代码实现 1) 传统的设计方 ...

  2. hdu 5883

    Alice is planning her travel route in a beautiful valley. In this valley, there are NN lakes, and MM ...

  3. 计组CPU设计实验关键材料和关键设计

    我记得这是2016春季学期搞得,参考和学习了很多别人的东西,这里小小的总结一下,逻辑性还不是太强,还需要好好整理 首先是指令集 CPU架构 外部接线架构 指令格式 机器状态自动机 这部分忘了,汗 这部 ...

  4. Linux 驱动框架---cdev字符设备驱动和misc杂项设备驱动

    字符设备 Linux中设备常见分类是字符设备,块设备.网络设备,其中字符设备也是Linux驱动中最常用的设备类型.因此开发Linux设备驱动肯定是要先学习一下字符设备的抽象的.在内核中使用struct ...

  5. 使用MCSManager搭建Minecraft服务器

    目录 一.准备工作 1.MCSManager Windows环境下安装 Linux安装 2.Minecraft服务端 3.Java 二.配置 1.登录面板 2.上传服务端 3.服务端的配置 三.开启服 ...

  6. 扫码登录 & 实现原理

    扫码登录 & 实现原理 二维码扫描登录是什么原理? https://time.geekbang.org/dailylesson/detail/100044032 xgqfrms 2012-20 ...

  7. NGK公链大事件盘点——回顾过去,展望未来!

    NGK公链构想广阔,愿景宏大,2020年10月NGK正式上线,同时NGK全球发布会正式启动,建立区块链生态体系. 早在这之前,NGK就经过了紧锣密鼓的数年缜密搭建. 2018年6月NGK底层系统技术原 ...

  8. 你真的知道typeof null的结果为什么是‘object‘吗?

    到目前为止,ECMAScript 标准中定义了8种数据类型,它们分别是Undefined.Null.Number.Boolean.String.Symbol.BigInt.Object. 为了判断变量 ...

  9. CF102920L Two Buildings【分治】【决策单调性】

    优秀的分治题目.是"2020-2021 ACM-ICPC, Asia Seoul Regional Contest"的一道题. Description There are \(n\ ...

  10. 一文学会Dockerfile语法

    接应上篇,续讲前文.今天咱来聊一下Dockerfile的使用 . 虽然可以通过docker commit命令来手动创建镜像,但是通过Dockerfile文件,可以帮助我们自动创建镜像,并且能够自定义创 ...