wpf学习笔记二 深入学习 xaml

本文介绍了XAML在UI设计中的作用及其与运行逻辑分离的优势,并详细解释了如何使用XAML属性为对象赋值,同时展示了TypeConvert类如何实现XAML属性与对象属性之间的映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值