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">

最新文章

  1. Python解析非标准JSON(Key值非字符串)
  2. PHP日常开发工具-Sublime应用
  3. 控件 UI: 字体的自动继承的特性, Style, ControlTemplate
  4. ruby 查询mysql方法
  5. 模拟 POJ 2993 Emag eht htiw Em Pleh
  6. php大力力 [030节] php设计系统后台菜单
  7. poj 1659 Frogs&#39; Neighborhood (DFS)
  8. UVa11404 - Palindromic Subsequence(区间DP+打印路径)
  9. 第一个Python程序的Hello Python,竟然有问题
  10. 深入理解 JavaScript 异步系列(4)—— Generator
  11. ELK介绍
  12. solr6.6教程-从mysql数据库中导入数据(三)
  13. [ gczdac ] 20190306 访者必阅
  14. umijs开发实践-不同页面交叉使用dva中的modal文件导致的错误
  15. FWT快速沃尔什变换学习笔记
  16. Flutter 开发小技巧
  17. 第九单元 利用vi编辑器创建和编辑正文文件
  18. linux基础(1)-终端&amp;shell类型&amp;命令&amp;文件系统&amp;命令帮助的获取
  19. C# 使用/配置Log4Net
  20. WP主题模板制作修改教程

热门文章

  1. TCP Wrappers原理及简单实验
  2. CodeForces 348D Turtles(LGV定理)题解
  3. Next.js 10
  4. 如何使用 js 写一个正常人看不懂的无聊代码
  5. HTML5 download 执行条件
  6. WebAssembly All In One
  7. ASCII Art
  8. Travis CI in Action
  9. TYLER ADAMS BRADBERRY的私人投资分享
  10. ubuntu无法连接有线网