WPF中的属性赋值和对象属性赋值

x33g5p2x  于9个月前 转载在 其他  
字(4.3k)|赞(0)|评价(0)|浏览(362)

1、attribute的形式和对象属性赋值

attribute 的形式是最为简单的,就是直接在属性上赋值,如下红色背景部分:

  1. <Window x:Class="MyWpf.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:MyWpf"xmlns:sys="clr-namespace:System;assembly=mscorlib"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800">
  2. <Window.Resources>
  3. <local:Human x:Key="human" Name="tom" Child="any"></local:Human>
  4. </Window.Resources>
  5. <Grid></Grid>
  6. </Window>

这是最简单的,如果要将对象进行属性赋值,就需要用 TypeConverter ,比如上述代码中有 Human 类如下,对数据进行转换:

  1. //使用HumanTypeConvert转换器
  2. [TypeConverter(typeof(HumanTypeConvert))]
  3. public classHuman
  4. {
  5. public string Name { get; set; }
  6. public Human Child { get; set; }
  7. }
  8. /// <summary>
  9. ///类型转换器
  10. /// </summary>
  11. public classHumanTypeConvert : TypeConverter
  12. {
  13. public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, objectvalue)
  14. {
  15. Human human = newHuman();
  16. human.Name =value.ToString();
  17. returnhuman;
  18. }
  19. }

然后就可以从 xamlresource 中获取 human 对象了:

  1. //xaml中resource的key
  2. var model = this.FindResource("human") asHuman;
  3. if(model != null)
  4.   {
  5. var str =model.Name;
  6. if(model.Child != null)
  7.     {
  8.       str +=model.Child.Name;
  9.     }
  10.     MessageBox.Show(str);
  11.   }

 2、属性标签

像是一般的属性比如 Text=“xxx” 这种赋值是简单的,但是如果是以一个复杂的对象,比如对象的列表就做不到了。如下所示的 RectangleFill 属性,对象很复杂可以用 xx.xx 的形式进行赋值。

  1. <Rectangle Width="50" Height="50" Stroke="Green">
  2.   <!--属性可以用xx.xx的形式-->
  3.   <Rectangle.Fill>
  4.     <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
  5.       <LinearGradientBrush.GradientStops>
  6.         <GradientStop Offset="0.2" Color="LightBlue" />
  7.         <GradientStop Offset="0.7" Color="DarkBlue" />
  8.         <GradientStop Offset="1.0" Color="Blue" />
  9.       </LinearGradientBrush.GradientStops>
  10.     </LinearGradientBrush>
  11.   </Rectangle.Fill>
  12. </Rectangle>

 3、属性标签的扩展

我们可以通过资源加载的方式来给属性赋值,如下所示我们在 button 组件下定以了一个名叫 MyLinearGradientBrush 的资源,然后我们通过 DynamicResource 动态加载资源。

  1. <Button Click="Button_Click" Width="200" Height="50" Name="button">
  2.   <Button.Resources>
  3.     <!--每个控件下也可以有资源,资源名称重复时取里的最近的资源类似html的css-->
  4.     <LinearGradientBrush x:Key="MyLinearGradientBrush" StartPoint="0,0" EndPoint="1,1" >
  5.       <GradientStop Offset="0.2" Color="LightBlue" />
  6.       <GradientStop Offset="0.7" Color="DarkBlue" />
  7.       <GradientStop Offset="1.0" Color="Blue" />
  8.     </LinearGradientBrush>
  9.   </Button.Resources>
  10.   <!--属性标签可以更复杂属性赋值,但是会使代码变得复杂-->
  11.   <Button.Content>
  12.     <Rectangle Width="50" Height="50" Stroke="Green" Fill="{DynamicResource ResourceKey=MyLinearGradientBrush}">
  13.     </Rectangle>
  14.   </Button.Content>
  15. </Button>

如果想要修改这个资源,则在后端代码如下:

  1. //修改DynamicResource动态数据源
  2.   //var linearGradientBrush = this.Resources["MyLinearGradientBrush"] as LinearGradientBrush
  3.   var linearGradientBrush = newLinearGradientBrush();
  4.   linearGradientBrush.StartPoint = new System.Windows.Point(0, 0);
  5.   linearGradientBrush.EndPoint = new System.Windows.Point(1, 1);
  6.   linearGradientBrush.GradientStops.Add(new GradientStop(System.Windows.Media.Color.FromRgb(97,168,13), 0.5));
  7.   linearGradientBrush.GradientStops.Add(new GradientStop(System.Windows.Media.Color.FromRgb(1, 2, 3), 1));
  8.   //这里只能取到window下的资源
  9.   //this.Resources["MyLinearGradientBrush"] = linearGradientBrush;
  10.   //获取控件下的资源
  11.   this.button.Resources["MyLinearGradientBrush"] = linearGradientBrush;

上面使用动态加载资源因为可以修改,使用StaticResource也可以加载资源但是一次加载不能修改,如下所示:

  1. <Window.Resources>
  2. <sys:String x:Key="stringHello">Hello WPF!</sys:String>
  3. </Window.Resources>
  4. <Grid>
  5. <!--标签扩展,这里使用静态资源-->
  6. <TextBlock Height="24" Width="120" Background="LightBlue" Text="{StaticResource ResourceKey=stringHello}" Margin="20,0,0,0" HorizontalAlignment="Left" />
  7. </Grid>

相关文章