在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>

 

 

posted on 2010-07-26 15:16  cwe  阅读(2132)  评论(0编辑  收藏  举报