WPF 基础

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">
原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14434277.html