三、事件处理程序与代码隐藏

例如,为一个Page添加一个Button控件,并为该Button添加事件名称Button_Click:

  1. <Page
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. x:Class="ExampleNamespace.ExamplePage">
  5. <Button Click="Button_Click" >Click Me!</Button>
  6. </Page>
  1. 然后为该Button的事件处理程序添加实现代码:
  1. namespace ExampleNamespace
  2. {
  3. public partial class ExamplePage
  4. {
  5. void Button_Click(object sender, RoutedEventArgs e)
  6. {
  7. Button b = e.Source as Button;
  8. b.Foreground = Brushes.Red;
  9. }
  10. }
  11. }

于是,这样就把Button连接到事件处理程序Button_Click。

四、命名元素

例如,在XAML文件中,为Grid命名为grid1:

  1. <Grid x:Name="grid1">
  2. </Grid>

或者,在VS的设计窗口中,设置Grid元素属性,将名称属性改为grid1。

现在,可以在C#代码中使用grid1了:

  1. MessageBox.Show(String.Format("The grid is {0}x{1} units in size.",
  2. grid1.ActualWidth, grid1.ActualHeight));

注意,与WinForm不同,WPF的控件不必每个都为之命名。

五、附加属性与附加事件——一个WPF例子

借用书中EightBalls的例子,其中UI如下:

在上面的TextBox输入问题,然后点击按钮,程序在下面的TextBox输出答案。

  1. <Window x:Class="EightBall.Window1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="Eight Ball Answer" Height="328" Width="412" >
  5. <Grid>
  6. <Grid.RowDefinitions>
  7. <RowDefinition Height="*" />
  8. <RowDefinition Height="Auto" />
  9. <RowDefinition Height="*" />
  10. </Grid.RowDefinitions>
  11. <Grid.Background>
  12. <LinearGradientBrush>
  13. <LinearGradientBrush.GradientStops>
  14. <GradientStop Offset="0.00" Color="Red" />
  15. <GradientStop Offset="0.50" Color="Indigo" />
  16. <GradientStop Offset="1.00" Color="Violet" />
  17. </LinearGradientBrush.GradientStops>
  18. </LinearGradientBrush>
  19. </Grid.Background>
  20. <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtQuestion"
  21. TextWrapping="Wrap" FontFamily="Verdana" FontSize="24"
  22. Grid.Row="0" >
  23. [Place question here.]
  24. </TextBox>
  25. <Button VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,0,20" Width="127" Height="23" Name="cmdAnswer"
  26. Click="cmdAnswer_Click"
  27. Grid.Row="1">
  28. Ask the Eight Ball
  29. </Button>
  30. <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtAnswer"
  31. TextWrapping="Wrap" IsReadOnly="True" FontFamily="Verdana" FontSize="24" Foreground="Green"
  32. Grid.Row="2">
  33. [Answer will appear here.]
  34. </TextBox>
  35. </Grid>
  36. </Window>

C#实现代码:

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Data;
  5. using System.Windows.Documents;
  6. using System.Windows.Media;
  7. using System.Windows.Media.Imaging;
  8. using System.Windows.Shapes;
  9. using System.Windows.Input;
  10.  
  11. namespace EightBall
  12. {
  13. /// <summary>
  14. /// Interaction logic for Window1.xaml
  15. /// </summary>
  16.  
  17. public partial class Window1 : Window
  18. {
  19.  
  20. public Window1()
  21. {
  22. InitializeComponent();
  23. }
  24.  
  25. private void cmdAnswer_Click(object sender, RoutedEventArgs e)
  26. {
  27. // Dramatic delay...
  28. this.Cursor = Cursors.Wait;
  29. System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
  30.  
  31. AnswerGenerator generator = new AnswerGenerator();
  32. txtAnswer.Text = generator.GetRandomAnswer(txtQuestion.Text);
  33. this.Cursor = null;
  34. }
  35.  
  36. }
  37. }

1.XAML指定一个语言功能,该功能的属性版本称为附加属性,事件版本称为附加事件。 从概念上讲,可以将附加属性和附加事件视为可以在任何 XAML 元素/对象实例上设置的全局成员。 但是,元素/类或更大的基础结构必须支持附加值的后备属性存储。在特性语法中,您可以采用“所有者类型.属性名”的形式指定附加属性。

2.在Grid.Property嵌套LinerGridientBrush,再嵌套LinearGradientBrush.GradientStops,并对此设置属性。

3.标记扩展:x:Type,x:Static,x:Array,x:Null。语法:{MarkupExtensionClass Argument}。

标记扩展继承自基类System.Windows.Markup.MarkupExtention。例如,

  1. <Button ... Foreground="{x:Static SystemColors.ActiveCaptionBrush}" >

这等同于将标记扩展嵌套到对象属性:

  1. <Button ... >
  2. <Button.Foreground>
  3. <x:Static Member="SystemColors.ActiveCaptionBrush"></x:Static>
  4. </Button.Foreground>
  5. </Button>

4.附加属性:通常用于容器中的控件。每个控件都有其固有的附加属性,取决于它所在的容器。例如:

  1. <TextBox Grid.Row=”0”>
  2. </TextBox>
  3. <Button Grid.Row=”1”>
  4. Ask the Eight Ball
  5. </Button>
  6. <TextBox Grid.Row=”2”>
  7. </TextBox>

附加属性不是真正的属性,它们被转换成方法调用:DefiningType.SetPropertyName()。如上代码,会被转换成:

  1. Grid.SetRow().
  1. Grid.SetRow(txtQuestion, 0);

5.嵌套元素:Window包含Grid,Grid包含TextBox和Button。

IList.Add();

IDictionary.Add();

ContentProperty:

例如,

  1. <LinearGradientBrush>
  2. <LinearGradientBrush.GradientStops>
  3. <GradientStop Offset="0.00" Color="Red" />
  4. <GradientStop Offset="0.50" Color="Indigo" />
  5. <GradientStop Offset="1.00" Color="Violet" />
  6. </LinearGradientBrush.GradientStops>
  7. </LinearGradientBrush>

GradientStops属性返回一个GradientStopCollection对象,而GradientStopCollection实现IList接口。因此,每个GradientStop调用IList.Add()方法。

上述XAML代码等同于:

  1. GradientStop gradientStop1 = new GradientStop();
  2. gradientStop1.Offset = 0;
  3. gradientStop1.Color = Colors.Red;
  4. IList list = brush.GradientStops;
  5. list.Add(gradientStop1);

6.Collection。某些属性可能支持不止一种Collection。因此,要添加标签指定Collection类:

  1. <LinearGradientBrush>
  2. <LinearGradientBrush.GradientStops>
  3. <GradientStopCollection>
  4. <GradientStop Offset="0.00" Color="Red" />
  5. <GradientStop Offset="0.50" Color="Indigo" />
  6. <GradientStop Offset="1.00" Color="Violet" />
  7. </GradientStopCollection>
  8. </LinearGradientBrush.GradientStops>
  9. </LinearGradientBrush>

WPF学习笔记2——XAML之2的更多相关文章

  1. WPF学习笔记1——XAML之1

    参考文献: http://msdn.microsoft.com/zh-cn/library/ms752059(v=vs.110).aspx <Pro WPF 4.5 in C# > 一.X ...

  2. WPF学习笔记——认识XAML

    Extensible Application Markup Language,XAML是一种声明性标记语言. 一.XAML语法概述 1,与XML类似,用尖括号标记元素 <StackPanel&g ...

  3. WPF学习笔记 - 在XAML里绑定

    Binding除了默认构造函数外,还有一个可以传入Path的构造函数,下面两种方式实现的功能是一样的. <TextBlock x:Name="currentFolder" D ...

  4. WPF学习笔记-用Expression Design制作矢量图然后导出为XAML

    WPF学习笔记-用Expression Design制作矢量图然后导出为XAML 第一次用Windows live writer写东西,感觉不错,哈哈~~ 1.在白纸上完全凭感觉,想象来画图难度很大, ...

  5. WPF 学习笔记-在WPF下创建托盘图标

    原文:WPF 学习笔记-在WPF下创建托盘图标 首先需要在项目中引用System.Windows.Forms,System.Drawing; using System; using System.Co ...

  6. WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决

    原文:WPF学习笔记(8):DataGrid单元格数字为空时避免验证问题的解决 如下图,在凭证编辑窗体中,有的单元格不需要数字,但如果录入数字后再删除,会触发数字验证,单元格显示红色框线,导致不能执行 ...

  7. WPF 学习笔记-设置属性使窗口不可改变大小

    原文:WPF 学习笔记-设置属性使窗口不可改变大小 调整Windows下的ResizeMode属性: ResizeMode = NoResize Resize属性是控制Windows是否可以改变大小, ...

  8. 【WPF学习笔记】之如何把数据库里的值读取出来然后显示在页面上:动画系列之(六)(评论处有学习资料及源码)

    (应博友们的需要,在文章评论处有源码链接地址,以及WPF学习资料.工具等,希望对大家有所帮助) ...... 承接系列五 上一节讲了,已经把数据保存到数据库并且删除数据,本讲是把已经存在的数据从数据库 ...

  9. [WPF学习笔记]动态加载XAML

    好久没写Blogs了,现在在看[WPF编程宝典],决定开始重新写博客,和大家一起分享技术. 在编程时我们常希望界面是动态的,可以随时变换而不需要重新编译自己的代码. 以下是动态加载XAML的一个事例代 ...

随机推荐

  1. Mysql数据库导入命令Source详解

    Mysql数据库导入命令Source详解 几个常用用例: 1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u root -p dat ...

  2. java_Cookie添加和删除

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...

  3. mysql控制流程函数

    1.case语句 select case 2 when 1 then '男' when 2 then '女' else 'xoap' end as result; 2.if语句 select if(1 ...

  4. windows 7 共享,未授予用户在此计算机上的请求登录类型

    刚刚重装了windows7,新下载的一个ghost版本,结果却不能共享,每次访问这台机器的共享都提示, 未授予用户在此计算机上的请求登录类型 这个情况好像是存在于win7访问win7,我用一台XP系统 ...

  5. CMS漏洞

    例1, discuz!后台弱口令/暴力破解 1.http://club.lenovo.com.cn/admin.php

  6. JS类型(1)_JS学习笔记(2016.10.02)

    js类型 js中的数据类型有undefined,boolean,number,string,null,object等6种,前5种为原始类型(基本类型),基本类型的访问是按值访问的,就是说你可以操作保存 ...

  7. next nextval

    1 KMP算法中next与nextval值的计算 以上两张图代表了next值的求法,本人总结后做如下叙述: 根据公式可知: next[1]=0 next[2]=1 next[3]的求法根据公式可以直接 ...

  8. nginx性能配置参数说明:

    nginx的配置:main配置段说明一.正常运行的必备配置: 1.user username [groupname]; 指定运行worker进程的用户和组 2.pid /path/to/pidfile ...

  9. 比较两个序列字典序(lexicographicallySmaller)

    bool lexicographicalSmaller(vector<int> a, vector<int> b) { int n = a.size(); int m = b. ...

  10. Spark技术内幕:Client,Master和Worker 通信源码解析

    http://blog.csdn.net/anzhsoft/article/details/30802603 Spark的Cluster Manager可以有几种部署模式: Standlone Mes ...