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

}

}

}

转载于:https://www.cnblogs.com/happygod/archive/2013/01/29/wpf.html

wpf学习笔记二 深入学习 xaml相关推荐

  1. 深度强化学习笔记(二)——Q-learning学习与二维寻路demo实现

    深度强化学习笔记(二)--Q-learning学习与二维寻路demo实现 文章目录 深度强化学习笔记(二)--Q-learning学习与二维寻路demo实现 前言 理论 什么是Q-Learning 算 ...

  2. wxpython应用程序对象与顶级窗口_wxPython学习笔记(二)

    如何创建和使用一个应用程序对象? 任何wxPython应用程序都需要一个应用程序对象.这个应用程序对象必须是类wx.App或其定制的子类的一个实例.应用程序对象的主要目的是管理幕后的主事件循环. 父类 ...

  3. css中怎么加入立体模型,CSS学习笔记二:css 画立体图形

    继上一次学了如何去运用css画平面图形,这一次学如何去画正方体,从2D向着3D学习,虽然有点满,但总是一个过程,一点一点积累,然后记录起来. Transfrom3D 在这一次中运用到了一下几种属性: ...

  4. qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)

    原博主博客地址:http://blog.csdn.net/qq21497936 本文章博客地址:http://blog.csdn.net/qq21497936/article/details/7851 ...

  5. [转载]dorado学习笔记(二)

    原文地址:dorado学习笔记(二)作者:傻掛 ·isFirst, isLast在什么情况下使用?在遍历dataset的时候会用到 ·dorado执行的顺序,首先由jsp发送请求,调用相关的ViewM ...

  6. PyTorch学习笔记(二)——回归

    PyTorch学习笔记(二)--回归 本文主要是用PyTorch来实现一个简单的回归任务. 编辑器:spyder 1.引入相应的包及生成伪数据 import torch import torch.nn ...

  7. tensorflow学习笔记二——建立一个简单的神经网络拟合二次函数

    tensorflow学习笔记二--建立一个简单的神经网络 2016-09-23 16:04 2973人阅读 评论(2) 收藏 举报  分类: tensorflow(4)  目录(?)[+] 本笔记目的 ...

  8. Scapy学习笔记二

    Scapy学习笔记二 Scapy Sniffer的用法: http://blog.csdn.net/qwertyupoiuytr/article/details/54670489 Scapy Snif ...

  9. Ethernet/IP 学习笔记二

    Ethernet/IP 学习笔记二 原文链接:http://wiki.mbalib.com/wiki/Ethernet/IP 1.通信模式 不同于源/目的通信模式,EtherNet/IP 采用生产/消 ...

最新文章

  1. docker通过镜像方式安装tomcat
  2. 前福娃集团营销总监-周胜哥箴言
  3. InputStreamReader/OutputStreamWriter乱码问题解决
  4. 某大学2021秋季学期Java期末考试范围概述
  5. python functools.wraps functools.partial实例解析
  6. java简单创建图片面板_图像界面编程简单窗体创建
  7. Python 新手入门引导
  8. 单链表的创建、测长、打印、插入、删除、排序及逆置
  9. 最受开发人员欢迎的JDBC接口
  10. 基于slate构建文档编辑器
  11. java调用公安接口_src 公安部PGIS在交警系统的应用,包括 的各种API 以及mysql对空间数据的支持 GIS program 261万源代码下载- www.pudn.com...
  12. matlab小波变换,图像处理
  13. 同时安装Office2016和Visio2016
  14. html制作钢铁侠心脏,钢铁侠胸部的“心脏”并非特效,那是怎么放进身体的?...
  15. 【数据机构】最短路径之Dijkstra算法(迪克斯特拉算法)
  16. python人工智能方向面试准备_人工智能入门学习路线及就业面试
  17. LVGL lv_cont 容器(8)
  18. camera 成像原理
  19. 解决spring coud打包报Singleton bean creation not allowed while singletons of this factory are in destruct
  20. 响应式网站建设有什么优势?

热门文章

  1. SAP OData $batch processing
  2. C#获取txt记事本内容,防止乱码情况
  3. DecimalFormat很强大
  4. NSARRAY的 内存管理
  5. linux系统安装arcsde,Linux操作系统安装ArcSDE10
  6. oracle的工具cmd,数据库命令行工具DBCLI
  7. 能测试快充真假的软件,苹果iOS 12可自行测试真假快充:山寨充电器将被洗牌
  8. 为何setRequestMethod(GET)不生效
  9. 快速入门系列之 Rust 语言 GitChat连接
  10. Axure 共享强制签出签入