wpf学习笔记三 深入学习 xaml

1、XAML 主要用于绘制UI界面,最大的优点是能使UI与运行逻辑分离开来,使得整个程序回到逻辑处理上来。

   每一个标签对应.NET Framework类库的一个控件类。通过设置标签的Attribute,不仅可以对标签所对应的控件    对象Property进行赋值,还可以声明名称空间,指定类名等

2、使用attribute给对象的属性赋值

   XAML是一种声明性语言,XAML编译器会为每个标签创建一个与之对应的对象,之后要对他的属性进行初始化    才会有意义。所以,每个标签除了声明对象就是初始化对象的属性--即给其属性赋值。赋值方法有两种:一种    是字符串简单赋值(在XAML中赋值),另外一种是使用属性元素进行复杂赋值(在.cs里面赋值)。下面的   例子用xaml赋值,

    1)前台代码:

<Window x:Class="firstApplicaton.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>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="*"/>

            <ColumnDefinition Width="120"/>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="100"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

         <Button x:Name="button1" Content="按钮1" Width="80"  Height="20" Grid.Column="0"

           Grid.Row="0" Background="Blue" />

         <Button x:Name="button2" Content="按钮2" Width="80" Height="20" Grid.Column="1"

           Grid.Row="1"  Background="Fuchsia"/>

        <Rectangle x:Name="正方形1"  Width="80" Height=" 80" Grid.Column="0" Grid.Row="1"

          Fill="Red"/>

    </Grid> </Window>

                          

      2)后台代码修改button 跟rectangle 的 填充色跟 背景颜色

       

using System;

using System.Collections.Generic;

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 firstApplicaton

{

    /// <summary>     /// Window1.xaml 的交互逻辑     /// </summary>

    public partial class Window1 : Window

    {

        public Window1()

        {

            InitializeComponent();

            //修改rectangle的填充色

            SolidColorBrush scb = new SolidColorBrush();

            scb.Color = Colors.Green;

            this.rec.Fill = scb;

            //修改button的背景色

            this.button1.Background = scb;

            this.button2.Background = scb;

        }

    }

}

                                       
3、TypeConvert类将XAML标签的Attribute与对象的Property进行映射

  1)TypeConvert: 提供一种将值类型转换为其他类型的统一方式。 TypeConverter 通常支持字符串到对象的

                  转换,目的是供设计环境中的属性编辑器使用或者是为了能够使用 XAML

                                   在xaml 语法中,元素英文就attribute value值都是sring类型的 但是 在相印的property却不都是

                                    string,为了能达到attribute与property的一一对应及相互操作 那就需要用上面的这个

                                    typeconvert类进行数据类型的转换。

      下面这个例子 自定义一个类 class1 在里面有两个属性,一个是name 另一个是 subclass ,在一下代码中可以看到subclass是class1类型的属性,但是在 xaml中属性却是string  现在问题来了  我们怎么能在。cs文件中把sting转换成 class1类型呢  这个时候就用到了 typeConvert  用他去重写 convertFrom 方法 来实现。

   2)xaml源码

<Window x:Class="firstApplicaton.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:a="clr-namespace:firstApplicaton"

    Title="Window1" Height="300" Width="300">

       <Window.Resources>

        <a:class1 x:Key="class1" Name="1" subclass="class2">

        </a:class1>

    </Window.Resources>

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="*"/>

            <ColumnDefinition Width="120"/>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="100"/>

            <RowDefinition Height="*"/>

        </Grid.RowDefinitions>

                 <Button x:Name="button1" Content="按钮1" Width="80"  Height="20" Grid.Column="0" Grid.Row="0" Background="Blue" Click="button1_Click" />

         <Button x:Name="button2" Content="按钮2" Width="80" Height="20" Grid.Column="1" Grid.Row="1"  Background="Fuchsia"/>

        <Rectangle x:Name="rec"  Width="80" Height=" 80" Grid.Column="0" Grid.Row="1" Fill="Red"/>

    </Grid>

</Window>

3)cs源码

 

using System;

using System.Collections.Generic;

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;

using System.ComponentModel;

 

namespace firstApplicaton

{

    /// <summary>     /// Window1.xaml 的交互逻辑     /// </summary>

    public partial class Window1 : Window

    {

        public Window1()

        {

            InitializeComponent();

            //修改rectangle的填充色

            SolidColorBrush scb = new SolidColorBrush();

            scb.Color = Colors.Green;

            this.rec.Fill = scb;

            //修改button的背景色

            this.button1.Background = scb;

            this.button2.Background = scb;

 

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

             class1  c =( class1) this.FindResource("class1");

 

            MessageBox.Show(c.subclass.Name);

 

                  

}

    }

    // 先托管 数据转换的类

    [TypeConverter(typeof(StringToClass1TypeConverter))]

    //目标类

    public class  class1

    {

        public string Name { get;set;}

        public class1 subclass{get;set;}

    }

    //重写数据类型转换 在默认的数据类型转换中会自动转换

    public class StringToCass1TypeConverter : TypeConverter

    {

 

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)

        {

 

            if (value is string)

            {

 

               class1 c = new class1();

 

                h.Name = value as string;

 

                return c;

 

            }

 

            return base.ConvertFrom(context, culture, value);

 

        }

 

    }

}

 

posted @ 2013-01-29 15:43  思@源  阅读(190)  评论(0编辑  收藏  举报