第一种方式 Attribute=Value
(1)优缺点:
(2)具体实现:
想要实现通过属性=value值来实现 从Human类中直接给Child赋值,转换为child.name=value的效果。
需要实现两个方法的重载:
CanConvertFrom():确保xaml语言不会报语法错误
ConvertFrom():将string赋值给human类的child.name
重写后将特性附加给Human类: [TypeConverterAttribute(typeof(NameToHumanTypeConverter))]
NameToHumanTypeConverter重写TypeConverter类
XAML:
Window.Resources通过资源将类以Key的方式存入,后在程序中以this.FindResource(key) as 类型取出
<Window x:Class="HelloWPF.MainWindow"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:HelloWPF" //本地名称空间mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><local:Human x:Key="human" Name="Jack" Child="Lucy"/>//名称空间有一个Human类,Key值为human</Window.Resources><Grid><Path Data="M 0,0 L 100,200 L 200,150 Z" Stroke="Black" Fill="Red" /><Button x:Name="btn1" Content="SHOW" Width="100" Click="btn_Click" Margin="346,140,0,0" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top"/><Button x:Name="btn2" Content="SHOW2" Click="btn2_Click" Margin="346,257,0,0" Height="20" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100"/></Grid></Window>
后台代码分部类:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Globalization;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace HelloWPF{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}private void btn_Click(object sender, RoutedEventArgs e){Human human = this.FindResource("human") as Human;if(human!=null){MessageBox.Show(human.Name);}}private void btn2_Click(object sender, RoutedEventArgs e){Human human = this.FindResource("human") as Human;if (human != null){MessageBox.Show(human.Child.Name);}}}[TypeConverterAttribute(typeof(NameToHumanTypeConverter))]public class Human{public string Name { get; set; }public Human Child { get; set; }}public class NameToHumanTypeConverter:TypeConverter{public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value){string name = value.ToString();Human human = new Human();human.Name = name;return human;}public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType){if (sourceType == typeof(string)){return true;}elsereturn base.CanConvertFrom(context, sourceType);}}}
效果图:点击Show出现Jack,点击Show2出现Lucy.
第二种方式 属性标签
添加复杂属性,将Button.Content更换为一个小矩形。矩形用LinearGradienBrush渐变笔刷进行绘制。
<Button x:Name="btn3" Width="100" Height="30" Margin="230,206,462,183"><!--Content--><Button.Content><Rectangle x:Name="rectangle1" Stroke="Black" Width="20" Height="20" OpacityMask="{DynamicResource {x:Static SystemColors.InactiveBorderBrushKey}}"><Rectangle.Fill><LinearGradientBrush><LinearGradientBrush.GradientStops><GradientStop Offset="0.2" Color="AliceBlue"/><GradientStop Offset="0.7" Color="Blue"/><GradientStop Offset="1" Color="Red"/></LinearGradientBrush.GradientStops></LinearGradientBrush></Rectangle.Fill></Rectangle></Button.Content></Button>
第三种方式 标签拓展
标签扩展 {Binding 元素名 Path} {StaticResource ResourceKey=xxx}
<Window x:Class="XAMLMarkExtension.MainWindow"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:sys="clr-namespace:System;assembly=mscorlib"xmlns:local="clr-namespace:XAMLMarkExtension"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><sys:String x:Key="HelloWorld">HelloWPF!</sys:String></Window.Resources><Grid Margin="4"><Grid.RowDefinitions><RowDefinition Height="24"/><RowDefinition Height="4"/><RowDefinition Height="24"/></Grid.RowDefinitions><TextBox x:Name="tb" Text="{Binding ElementName=sld,Path=Value}"/><Slider x:Name="sld" Grid.Row="2" Value="50" Maximum="100" Minimum="0"/><TextBlock Height="24" Width="120" Background="LightBlue"Text="{StaticResource ResourceKey=HelloWorld}" Margin="312,260,352,-259" Grid.Row="2"/></Grid></Window>
