WPF学习旅程(三)——在XAML中为对象的属性赋值
2010-07-26 15:16
573 查看
在XAML中为对象的属性赋值有三种方法:
1、attribute=“value”的形式:
1.1简单属性赋值
<Window x:Class="MultyProcess.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Label Height="28" Margin="48,77,110,0" Name="lblHello" VerticalAlignment="Top">Label</Label>
<Button Margin="48,127,0,112" Name="button1" HorizontalAlignment="Left" Width="75" Click="button1_Click">Button</Button>
</Grid>
</Window>
1.2使用对象属性怎么赋值
前面是一些简单属性的赋值,我们除了简单属性,还有复杂的属性,如对象,下面就是如何给对象赋值
xmal代码:
<Window x:Class="MultyProcess.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MultyProcess"
Title="Window2" Height="300" Width="300">
<Window.Resources>
<local:Human x:Key="human" Name="吕伟"></local:Human>
</Window.Resources>
<Grid>
<Button Height="23" HorizontalAlignment="Left" Margin="51,89,0,0" Click="button1_Click" Name="button1" VerticalAlignment="Top" Width="75">Button</Button>
</Grid>
</Window>
。cs代码
/// <summary>
/// Window2.xaml 的交互逻辑
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Human human = this.FindResource("human") as Human;
MessageBox.Show(human.Name);
}
}
public class Human
{
public string Name { get; set; }
public Human child { get; set; }
}
1.3怎么给对象赋值对象属性
在前一个示例中human类中的属性child没有被赋值,如果赋值为字符串会编译出错,那么只有通过实现typeConverter来进行扩展示例如下
[TypeConverter(typeof(StringToHumanConvertor))]
public class Human
{
public string Name { get; set; }
public Human child { get; set; }
}
public class StringToHumanConvertor : TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
string name = value.ToString();
Human child = new Human();
child.Name = name;
return child;
}
}
2、属性标签的方式给属性赋值
<Window x:Class="MultyProcess.AttributeValue2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AttributeValue2" Height="300" Width="300">
<Grid>
<Button Height="30" Margin="46,0,132,94" VerticalAlignment="Bottom">
<Button.Content>
<Rectangle Width="20" Height="20" Fill="Green" Stroke="Black"></Rectangle>
</Button.Content>
</Button>
<Rectangle Height="100" Stroke="Blue" Margin="28,21,50,0" VerticalAlignment="Top">
<Rectangle.Fill>
<LinearGradientBrush>
<LinearGradientBrush.StartPoint>
<Point X="0" Y="0"></Point>
</LinearGradientBrush.StartPoint>
<LinearGradientBrush.EndPoint>
<Point X="1" Y="1"></Point>
</LinearGradientBrush.EndPoint>
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Offset="0.2" Color="LightBlue"></GradientStop>
<GradientStop Offset="0.7" Color="DarkBlue"></GradientStop>
<GradientStop Offset="1.0" Color="Blue"></GradientStop>
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Window>
3、标签扩展
标签扩展有staticresource,binding等五种,示例如下:
<Window x:Class="WPFXAMLCode.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ExternalDll="clr-namespace:ExternalDll;assembly=ExternalDll"
xmlns:local="clr-namespace:WPFXAMLCode;assembly="
xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window1" Height="300" Width="600"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<SolidColorBrush x:Key="windowLevelResourceBlueBrush" Color="Blue"/>
</Window.Resources>
<StackPanel x:Name="sp1" Orientation="Vertical">
<StackPanel.Resources>
<local:LocalClass x:Key="localClass1" AnIntProp="5"/>
<collections:Hashtable x:Key="ht1">
<sys:Int32 x:Key="key1">1</sys:Int32>
<sys:Int32 x:Key="key2">2</sys:Int32>
</collections:Hashtable>
<SolidColorBrush x:Key="parentLevelResourceOrangeBrush" Color="Orange"/>
</StackPanel.Resources>
<local:LocalControl/>
<ExternalDll:ExternalDllControl/>
<Rectangle Fill="{x:Null}" Stroke="Black" StrokeThickness="2" Height="20"/>
<Button Width="auto" Content="引用window级别资源" Background="{StaticResource windowLevelResourceBlueBrush}"/>
<Button Width="auto" Content="引用window级别资源" Background="{StaticResource windowLevelResourceBlueBrush}"/>
<Button Width="auto" Content="引用Application资源" Background="{StaticResource appLevelResourceGreenBrush}"/>
<Button Width="auto" Content="引用StackPanel级别资源" Background="{StaticResource parentLevelResourceOrangeBrush}"/>
<Button Width="auto" Content="引用LocalResourceDictionary.xaml的资源">
<Button.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="LocalResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Button.Resources>
<Button.Background>
<StaticResourceExtension ResourceKey="seperateResourceFilePinkBrush"/>
</Button.Background>
</Button>
</StackPanel>
</Window>
1、attribute=“value”的形式:
1.1简单属性赋值
<Window x:Class="MultyProcess.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Label Height="28" Margin="48,77,110,0" Name="lblHello" VerticalAlignment="Top">Label</Label>
<Button Margin="48,127,0,112" Name="button1" HorizontalAlignment="Left" Width="75" Click="button1_Click">Button</Button>
</Grid>
</Window>
1.2使用对象属性怎么赋值
前面是一些简单属性的赋值,我们除了简单属性,还有复杂的属性,如对象,下面就是如何给对象赋值
xmal代码:
<Window x:Class="MultyProcess.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MultyProcess"
Title="Window2" Height="300" Width="300">
<Window.Resources>
<local:Human x:Key="human" Name="吕伟"></local:Human>
</Window.Resources>
<Grid>
<Button Height="23" HorizontalAlignment="Left" Margin="51,89,0,0" Click="button1_Click" Name="button1" VerticalAlignment="Top" Width="75">Button</Button>
</Grid>
</Window>
。cs代码
/// <summary>
/// Window2.xaml 的交互逻辑
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Human human = this.FindResource("human") as Human;
MessageBox.Show(human.Name);
}
}
public class Human
{
public string Name { get; set; }
public Human child { get; set; }
}
1.3怎么给对象赋值对象属性
在前一个示例中human类中的属性child没有被赋值,如果赋值为字符串会编译出错,那么只有通过实现typeConverter来进行扩展示例如下
[TypeConverter(typeof(StringToHumanConvertor))]
public class Human
{
public string Name { get; set; }
public Human child { get; set; }
}
public class StringToHumanConvertor : TypeConverter
{
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
string name = value.ToString();
Human child = new Human();
child.Name = name;
return child;
}
}
2、属性标签的方式给属性赋值
<Window x:Class="MultyProcess.AttributeValue2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="AttributeValue2" Height="300" Width="300">
<Grid>
<Button Height="30" Margin="46,0,132,94" VerticalAlignment="Bottom">
<Button.Content>
<Rectangle Width="20" Height="20" Fill="Green" Stroke="Black"></Rectangle>
</Button.Content>
</Button>
<Rectangle Height="100" Stroke="Blue" Margin="28,21,50,0" VerticalAlignment="Top">
<Rectangle.Fill>
<LinearGradientBrush>
<LinearGradientBrush.StartPoint>
<Point X="0" Y="0"></Point>
</LinearGradientBrush.StartPoint>
<LinearGradientBrush.EndPoint>
<Point X="1" Y="1"></Point>
</LinearGradientBrush.EndPoint>
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Offset="0.2" Color="LightBlue"></GradientStop>
<GradientStop Offset="0.7" Color="DarkBlue"></GradientStop>
<GradientStop Offset="1.0" Color="Blue"></GradientStop>
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Window>
3、标签扩展
标签扩展有staticresource,binding等五种,示例如下:
<Window x:Class="WPFXAMLCode.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ExternalDll="clr-namespace:ExternalDll;assembly=ExternalDll"
xmlns:local="clr-namespace:WPFXAMLCode;assembly="
xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Window1" Height="300" Width="600"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<SolidColorBrush x:Key="windowLevelResourceBlueBrush" Color="Blue"/>
</Window.Resources>
<StackPanel x:Name="sp1" Orientation="Vertical">
<StackPanel.Resources>
<local:LocalClass x:Key="localClass1" AnIntProp="5"/>
<collections:Hashtable x:Key="ht1">
<sys:Int32 x:Key="key1">1</sys:Int32>
<sys:Int32 x:Key="key2">2</sys:Int32>
</collections:Hashtable>
<SolidColorBrush x:Key="parentLevelResourceOrangeBrush" Color="Orange"/>
</StackPanel.Resources>
<local:LocalControl/>
<ExternalDll:ExternalDllControl/>
<Rectangle Fill="{x:Null}" Stroke="Black" StrokeThickness="2" Height="20"/>
<Button Width="auto" Content="引用window级别资源" Background="{StaticResource windowLevelResourceBlueBrush}"/>
<Button Width="auto" Content="引用window级别资源" Background="{StaticResource windowLevelResourceBlueBrush}"/>
<Button Width="auto" Content="引用Application资源" Background="{StaticResource appLevelResourceGreenBrush}"/>
<Button Width="auto" Content="引用StackPanel级别资源" Background="{StaticResource parentLevelResourceOrangeBrush}"/>
<Button Width="auto" Content="引用LocalResourceDictionary.xaml的资源">
<Button.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="LocalResourceDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Button.Resources>
<Button.Background>
<StaticResourceExtension ResourceKey="seperateResourceFilePinkBrush"/>
</Button.Background>
</Button>
</StackPanel>
</Window>
相关文章推荐
- WPF 之 XAML语法,对象属性赋值(简单赋值、TypeConverter 标签属性与对象属性映射、标记扩展),引用外部库
- 无废话WPF系列3: XAML中为对象属性赋值
- 【Java EE 学习 69 上】【struts2】【paramsPrepareParamsStack拦截器栈解决model对象和属性赋值冲突问题】
- WPF学习笔记--xaml属性介绍
- WPF学习笔记 - 与.Net对象属性绑定
- Wpf 对象属性赋值---属性标签
- XAML属性赋值转换之谜(WPF XAML语法解密)
- XAML中为对象属性赋值
- WPF中XMAL对象属性赋值及属性类型转换机制
- Wpf 使用标签扩展为对象的属性赋值。
- 选中对象【WPF】自定义控件之依赖属性
- js面向对象学习笔记之九(BOM 与 DOM 中常用属性分析)
- C++学习笔记->类、对象、属性、方法
- wpf基础快速学习 一 xaml 基础知识学习
- 在Flex中使用JSON对象为属性赋值
- javascript学习(九)对象属性的特性
- 继承对象的属性访问和赋值是有差别的
- Java 对象之间相同属性的赋值
- [深入学习C#]利用反射给对象赋值
- 一起谈.NET技术,晚绑定场景下对象属性赋值和取值可以不需要PropertyInfo