第一种方式 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 类型取出

  1. <Window x:Class="HelloWPF.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:HelloWPF" //本地名称空间
  7. mc:Ignorable="d"
  8. Title="MainWindow" Height="450" Width="800">
  9. <Window.Resources>
  10. <local:Human x:Key="human" Name="Jack" Child="Lucy"/>//名称空间有一个Human类,Key值为human
  11. </Window.Resources>
  12. <Grid>
  13. <Path Data="M 0,0 L 100,200 L 200,150 Z" Stroke="Black" Fill="Red" />
  14. <Button x:Name="btn1" Content="SHOW" Width="100" Click="btn_Click" Margin="346,140,0,0" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top"/>
  15. <Button x:Name="btn2" Content="SHOW2" Click="btn2_Click" Margin="346,257,0,0" Height="20" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100"/>
  16. </Grid>
  17. </Window>

后台代码分部类:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. namespace HelloWPF
  17. {
  18. /// <summary>
  19. /// MainWindow.xaml 的交互逻辑
  20. /// </summary>
  21. public partial class MainWindow : Window
  22. {
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. }
  27. private void btn_Click(object sender, RoutedEventArgs e)
  28. {
  29. Human human = this.FindResource("human") as Human;
  30. if(human!=null)
  31. {
  32. MessageBox.Show(human.Name);
  33. }
  34. }
  35. private void btn2_Click(object sender, RoutedEventArgs e)
  36. {
  37. Human human = this.FindResource("human") as Human;
  38. if (human != null)
  39. {
  40. MessageBox.Show(human.Child.Name);
  41. }
  42. }
  43. }
  44. [TypeConverterAttribute(typeof(NameToHumanTypeConverter))]
  45. public class Human
  46. {
  47. public string Name { get; set; }
  48. public Human Child { get; set; }
  49. }
  50. public class NameToHumanTypeConverter:TypeConverter
  51. {
  52. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  53. {
  54. string name = value.ToString();
  55. Human human = new Human();
  56. human.Name = name;
  57. return human;
  58. }
  59. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  60. {
  61. if (sourceType == typeof(string))
  62. {
  63. return true;
  64. }
  65. else
  66. return base.CanConvertFrom(context, sourceType);
  67. }
  68. }
  69. }

效果图:点击Show出现Jack,点击Show2出现Lucy.
image.png

第二种方式 属性标签

添加复杂属性,将Button.Content更换为一个小矩形。矩形用LinearGradienBrush渐变笔刷进行绘制。

  1. <Button x:Name="btn3" Width="100" Height="30" Margin="230,206,462,183">
  2. <!--Content-->
  3. <Button.Content>
  4. <Rectangle x:Name="rectangle1" Stroke="Black" Width="20" Height="20" OpacityMask="{DynamicResource {x:Static SystemColors.InactiveBorderBrushKey}}">
  5. <Rectangle.Fill>
  6. <LinearGradientBrush>
  7. <LinearGradientBrush.GradientStops>
  8. <GradientStop Offset="0.2" Color="AliceBlue"/>
  9. <GradientStop Offset="0.7" Color="Blue"/>
  10. <GradientStop Offset="1" Color="Red"/>
  11. </LinearGradientBrush.GradientStops>
  12. </LinearGradientBrush>
  13. </Rectangle.Fill>
  14. </Rectangle>
  15. </Button.Content>
  16. </Button>

第三种方式 标签拓展

标签扩展 {Binding 元素名 Path} {StaticResource ResourceKey=xxx}

  1. <Window x:Class="XAMLMarkExtension.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:sys="clr-namespace:System;assembly=mscorlib"
  7. xmlns:local="clr-namespace:XAMLMarkExtension"
  8. mc:Ignorable="d"
  9. Title="MainWindow" Height="450" Width="800">
  10. <Window.Resources>
  11. <sys:String x:Key="HelloWorld">HelloWPF!</sys:String>
  12. </Window.Resources>
  13. <Grid Margin="4">
  14. <Grid.RowDefinitions>
  15. <RowDefinition Height="24"/>
  16. <RowDefinition Height="4"/>
  17. <RowDefinition Height="24"/>
  18. </Grid.RowDefinitions>
  19. <TextBox x:Name="tb" Text="{Binding ElementName=sld,Path=Value}"/>
  20. <Slider x:Name="sld" Grid.Row="2" Value="50" Maximum="100" Minimum="0"/>
  21. <TextBlock Height="24" Width="120" Background="LightBlue"
  22. Text="{StaticResource ResourceKey=HelloWorld}" Margin="312,260,352,-259" Grid.Row="2"/>
  23. </Grid>
  24. </Window>