WPF视频教程系列笔记

WPF视频教程系列笔记

视频二:XAML基础

                        1.*元素 <Window></Window>,<Page></Page>,<Application></Application>

                        2. 名称空间 很重要。 默认名称空间:xmlns="" ;   另外名称空间:  xmlns:x=""   ; x是名称空间前缀 

                            x:Class="WpfApplication1.MainWindow" 是继续自Window类。

                        3. 简单属性:

                             实例1:简单属性

                             <Grid Name="Grid1"></Grid>

                             this.Title=this.Grid1.Name;

                             实例2:复杂属性,属性元素的方法

                             <Grid >

                             <Grid.Name>Grid1</Grid.Name>

                             <Button></Button>

                              </Grid>

                         4. 渐变的颜色

                              <Grid  Name="Grid1">

                                      <Grid.Background>

                                            <LinearGradientBrush>

                                              <LinearGradientBrush.GradientStops>

                                                 <GradientStop Offset="0.00" Color="Red"/>                                             

                                                 <GradientStop Offset="0.50" Color="Indigo"/>

                                                 <GradientStop Offset="1.00" Color="Violet"/>                                            

                                              </LinearGradientBrush.GradientStops>

                                              </LinearGradientBrush>

                                       </Grid.Background>

                               </Grid>

                           5. 附加的属性

                               <Button Content="Button1" HorizontalAlignment="Left" Margin="215,60,0,0" VerticalAlignment="Top" Width="75"/>
                              <Button Content="Button2" Grid.Row="1"  HorizontalAlignment="Left" Margin="215,38,0,0" VerticalAlignment="Top" Width="75"/>
                             <Button Content="Button3" Grid.Row="2" HorizontalAlignment="Left" Margin="215,47,0,0" VerticalAlignment="Top" Width="75" />

                            其中:Grid.Row 默认从0开始,默认不写, 这是一个附加属性。

                            6.特殊字符

                                特殊字符串‘<’使用 ‘&lt’代替 ,特殊字符串‘>’使用 ‘&gt’代替

                                空格字符:xml:space="preserve" 添加属性,可以完整显示空格。

                                <TextBox HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="465" xml:space="preserve">where this is "         "...</TextBox>

                            7. XAML事件

                               <Button Content="Button1" HorizontalAlignment="Left" Margin="215,60,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

                                按钮的单击事件

视频三:只使用代码创建WPF应用程序(缺点,慢;优点,*。)

                          补充知识点:使用partial关键字的类是可以互相补充的类。 关键WPF有三种方式:a.仅使用代码创建;b.仅使用页面创建;c.两者结合,相互补充。

                         

using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;

namespace _2014_10_09_WPF
{
    class Window1:Window
    {
        private Button button1;

        public Window1()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
             //设置窗体
            this.Width = 285;
            this.Height = 250;
            this.Left = this.Top = 100;
            this.Title = "Code_Only Window";
            //创建停靠面板对象
            DockPanel panel = new DockPanel();
            //创建按钮对象
            button1 = new Button();
            button1.Content = "Please click me.";
            button1.Margin = new Thickness(30);
            //事件
            button1.Click+=button1_Click;

            ////添加控件
            IAddChild container = panel;
            container.AddChild(button1);
            container = this;
            container.AddChild(panel);
        }
        private void button1_Click(object sender,RoutedEventArgs e)
        {
            button1.Content = "Thank you.";
        }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace _2014_10_09_WPF
{
    class Program:Application
    {
        [STAThread]
        static void Main()
        {
            Program app = new Program();
            app.MainWindow = new Window1();
            app.MainWindow.ShowDialog();
        }
    }
}
View Code

视频四:使用代码和未经编译的外部标记XAML文件,创建WPF应用程序

           代码如下:

           

////文件名称:MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Windows.Markup;

namespace _2014_10_10使用代码和未经编译的标记XAML创建WPF应用程序
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Button myButton;
        public MainWindow()
        {
            InitializeComponent();
        }
        public MainWindow(string xamlFile)
        {
            //设置窗体
            this.Width = this.Height = 285;
            this.Left = this.Top = 100;
            this.Title = "Dynamically loaded XAML";

            //从外部的一个XAML文件里面获取XAML内容
            DependencyObject rootElement;
            using (FileStream fs = new FileStream(xamlFile, FileMode.Open)) 
            {
                rootElement = (DependencyObject)XamlReader.Load(fs);
            }
            this.Content = rootElement;
            myButton =(Button)LogicalTreeHelper.FindLogicalNode(rootElement,"button1");
            myButton.Click += myButton_Click;
        }
        private void myButton_Click(object sender,RoutedEventArgs e)
        {
            myButton.Content = "thank you.";

        }
    }
}
View Code
////文件名称:Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace _2014_10_10使用代码和未经编译的标记XAML创建WPF应用程序
{
    class Program:Application
    {
        [STAThread()]
        static void Main()
        {
            Program app = new Program();
            app.MainWindow = new MainWindow("MainWindow.xaml");
            app.MainWindow.ShowDialog();
        }
    }
}
View Code

视频五:使用StackPanel面板进行简单的布局(WPF布局原则,使用StackPanel布局,Border控件:边距,边缘,边的大小) 堆栈面板,默认情况下是列排序方式。(垂直方向)

            布局控件的使用:实例代码

            

          

<Window x:Class="_2014_10_10使用StackPanel面板进行简单布局.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="706.7">
    <Border BorderBrush="Black" BorderThickness="3" Margin="44.5,0,224.5,0" Padding="10" CornerRadius="10">
    <StackPanel Name="stackPanel1" Orientation="Horizontal"  HorizontalAlignment="Center">
        <Label Content="Label"  />
        <Button Content="Button1" Margin="10,0,10,0" MinWidth="20" MaxWidth="200" />
        <Button Content="Button2" />
        <Button Content="Button3" />
        <Button Content="Button4" />
        </StackPanel>
    </Border>
    


</Window>
View Code

视频六:使用WrapPanel 面板和DockPanel 面板使用 (嵌套布局容器)

           WrapPanel 默认情况是水平排序,行排序。wrap:包裹。 wrap up 伪装。

           DockPanel 一些使用方法,代码如下:

          

<Window x:Class="使用WrapPanel_面板和DockPanel_面板使用__嵌套布局容器_.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel  LastChildFill="True">
        <Button Content="Top Button"  DockPanel.Dock="Top"  />
        <Button Content="Bottom Button" DockPanel.Dock="Bottom"/>
        <Button Content="Left Button"  DockPanel.Dock="Left" Width="75"/>
        <Button Content="Right Button" DockPanel.Dock="Right" Width="75"/>
        <Button Content="Remaining Button" />
    </DockPanel>   

</Window>
View Code

           嵌套布局容器实例:

<Window x:Class="使用WrapPanel_面板和DockPanel_面板使用__嵌套布局容器_.BasicDialogBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="BasicDialogBox" Height="300" Width="300">
    <DockPanel  Name="dockPanel1">
        <StackPanel Name="stackPanel" DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Content="OK" Margin="10,10,2,10" Padding="3" />
            <Button Content="Cancel" Margin="2,10,10,10" Padding="3" />

        </StackPanel>
        <TextBlock Name="txtBox1" DockPanel.Dock="Top">This is a test.</TextBlock>
    </DockPanel>

</Window>
View Code

视频七: Grid面板 (调整行与列,布局舍入,跨越行与列,分割窗口,共享尺寸组)

             调整行与列:Grid.Row="0"  Grid.Column="1"  调整列的宽度:a. Width="200"  b. Width="auto"   c. Width="*"  Width="2*"   (a最不可取)

             布局舍入: UseLayoutRounding="True"  解决图片模糊问题

             跨越行与列: Grid.RowSpan="2",Grid.ColumnSpan="2"

             分割窗口:分割器对象: <GridSplitter Grid.Column="1"  Grid.RowSpan="2" Width="3" VerticalAlignment="Stretch" HorizontalAlignment="Center" />

             共享尺寸组:SharedSizeGroup="TextLable"  需要同名

<Window x:Class="_2014_10_10Grid面板.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid ShowGridLines="True" UseLayoutRounding="True" >
        <Grid.RowDefinitions>
            <RowDefinition ></RowDefinition>
            <RowDefinition ></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="50"></ColumnDefinition>
            <ColumnDefinition Width="auto"></ColumnDefinition>
            <ColumnDefinition Width="2*" MinWidth="50"></ColumnDefinition>
            <ColumnDefinition Width="4*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Button Content="Left Top" Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" Margin='3'/>
        <Button Content="Center Top" Grid.Row="0" Grid.Column="2"  Margin="3"/>
        <Button Content="Right Bottom" Grid.Row="1"  Grid.ColumnSpan="2" Grid.Column="2"  Margin="3"/>
        <GridSplitter Grid.Column="1"  Grid.RowSpan="2" Width="3" VerticalAlignment="Stretch" HorizontalAlignment="Center" />
    </Grid>
</Window>
View Code

视频八:使用Canvas面板进行基于坐标的布局 (Canvas面板,Z 顺序,InkCanvas元素)

           Canvas面板位置:<Canvas Name="canvas1">
            <Button Canvas.Left="136" Canvas.Top="20" Content="Button" Width="75"/>
        </Canvas>

            控件重叠的处理方案:默认情况下都是,Canvas.ZIndex="0"  为0

<Window x:Class="_2014_10_10使用Canvas面板进行基于坐标的布局.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Canvas Name="canvas1">
            <Button Name="button1" Canvas.Left="136" Canvas.ZIndex="1"  Canvas.Top="20" Content="Button" Width="75"/>
            <DataGrid Height="166" Width="295" Panel.ZIndex="2" >
            </DataGrid>
            <Button  Canvas.Left="353"  Canvas.Top="77" Content="Button" Width="75" Click="Button_Click"/>
        </Canvas>
    </Grid>
    
</Window>
View Code

            InkCanvas 元素 :接收手写笔的输入,支持鼠标直接画图

            <InkCanvas EditingMode="GestureOnly">(根据设置的模式不一样,有不同的效果。)</InkCanvas>

           

<Window x:Class="_2014_10_10使用Canvas面板进行基于坐标的布局.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0">
            <ComboBox Name="EditingMode" Margin="5" SelectionChanged="EditingMode_SelectionChanged" ></ComboBox>
        </StackPanel>
   <InkCanvas EditingMode="None" Grid.Row="1" Name="inkCanvas" Background="#FF1BB444">
       
        <Button InkCanvas.Left="50" InkCanvas.Top="50" Content="Button1"></Button>
   </InkCanvas>
    </Grid>
</Window>
View Code
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.Shapes;

namespace _2014_10_10使用Canvas面板进行基于坐标的布局
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (InkCanvasEditingMode item in Enum.GetValues(typeof(InkCanvasEditingMode)))
            {
                EditingMode.Items.Add(item);
            }
            EditingMode.SelectedIndex = 0;
        }

        private void EditingMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            inkCanvas.EditingMode = (InkCanvasEditingMode)this.EditingMode.SelectedValue;
        }
    }
}
View Code

 视频九:理解路由事件

            路由事件出现的三种方式:

            1. 直接路由事件

            2. 冒泡路由事件:向上传递 MouseUp="SomethingClicked"  与隧道路由事件是成对存在的,只是加了一个前缀Preview.

 protected int eventCountr = 0;
        private void SometingClicked(object sender,RoutedEventArgs e )
        {
            eventCountr++;
            string message = "#" + eventCountr.ToString() + ":
"
                + " Sender:" + sender.ToString() + ":
"
                + " Source:" + e.Source + ":
"
                + " Original Source:" + e.OriginalSource + ":
"
                + " Event:" + e.RoutedEvent + ":
";
            this.listbox1.Items.Add(message);
            e.Handled = (bool)chkHandle.IsChecked;
        }
View Code

            3. 隧道路由事件:向下传递 PreviewMouseUp="SomethingClicked" 隧道事件是以Preview开头。

视频十:键盘输入(WPF事件类型,键盘输入)

            WPF事件类型:五种,a. 生命周期事件(元素初始化,及卸载的时候触发);鼠标事件(鼠标动作);键盘事件(键盘动作);手写笔事件();多点触控事件。

             键盘输入: PreviewKeyDown(隧道路由事件),KeyDown(冒泡路由事件),PreviewTextInput(隧道路由事件),TextInput(接收文本,冒泡),PreviewKeyUp(释放按键,隧道),KeyUp(冒泡)

            

Window x:Class="_2014_10_11_10键盘输入.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid >

        <TextBox Focusable="True"  TabIndex="0" VerticalAlignment="Top" Grid.ZIndex="20"  Height="20" Width="50" Name="TextBox" PreviewKeyDown="KeyEvent" KeyDown="KeyEvent" PreviewTextInput="TextBox_PreviewTextInput" PreviewKeyUp="KeyEvent" KeyUp="KeyEvent" TextChanged="TextBox_TextChanged" ></TextBox>
        <ListBox VerticalAlignment="Bottom" Name="lstMessages"></ListBox>
    </Grid>
</Window>
View Code
using System;
using System.Collections.Generic;
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 _2014_10_11_10键盘输入
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void KeyEvent(object sender, KeyEventArgs e)
        {
            string message = " Event:"+ e.RoutedEvent+"  " +"Key:"+e.Key;
            lstMessages.Items.Add(message);
        }

        private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            string message = " Event:" + e.RoutedEvent + "  " + "Text:" + e.Text;
            lstMessages.Items.Add(message);
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            string message = " Event:" + e.RoutedEvent;
            lstMessages.Items.Add(message);
        }
    }
}
View Code

             焦点的获取及层次: Focusable="True"  TabIndex="0"         

 视频十一:鼠标输入(鼠标单击,捕获鼠标,鼠标拖放)  MouseEventArgs

               MouseEnter MouseLeave (直接事件)

               PreviewMouseMove(隧道路由事件)

               MouseMove(冒泡路由事件)  

               鼠标单击:PreviewMouseLeftButtonDown (左键)  PreviewMouseRightButtonDown(右键)

               鼠标的锁定,及鼠标的坐标。

<Window x:Class="_2014_10_11_11鼠标输入.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Rectangle Name="rect" Fill="LightBlue" MouseMove="rect_MouseMove"></Rectangle>
        <Button Grid.Row="1" Name="cmdCapture" Click="cmdCapture_Click">Capture the Mouse</Button>
        <TextBlock Name="lblInfo" Grid.Row="2"></TextBlock>
    </Grid>
</Window>
View Code
using System;
using System.Collections.Generic;
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 _2014_10_11_11鼠标输入
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private void rect_MouseMove(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition(this);
            this.lblInfo.Text=("You are at ")+pt.X+","+pt.Y;
        }

        private void cmdCapture_Click(object sender, RoutedEventArgs e)
        {
            Mouse.Capture(this.rect);///锁定鼠标,只有当焦点移开后,才可以继续操作。
            this.cmdCapture.Content = "Mouse is now captured";
        }
    }
}
View Code

   鼠标的拖拽:默认情况下,textbox支持拖拽功能,选中内容+shift(剪切).选中内容+Ctrl(复制). 只需根据如下代码,可以将lable控件实现拖拽功能。在XAML中,lable控件需要设置允许接收数据。AllowDrop="True"

using System;
using System.Collections.Generic;
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.Shapes;

namespace _2014_10_11_11鼠标输入
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void lblSoure_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Label lbl =(Label) sender;
            DragDrop.DoDragDrop(lbl,lbl.Content,DragDropEffects.Copy);
        }

        private void Label_Drop(object sender, DragEventArgs e)
        {
            ((Label)sender).Content = e.Data.GetData(DataFormats.Text);
        }
    }
}
View Code
<Window x:Class="_2014_10_11_11鼠标输入.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.RowDefinitions>
            <RowDefinition ></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox Text="123456789" Grid.Row="0"  HorizontalAlignment="Left" Height="23" TextWrapping="Wrap"  VerticalAlignment="Top" Width="120"/>
        <TextBox  Text="ABCDEFG"  Grid.Row="1"  HorizontalAlignment="Left" Height="23" TextWrapping="Wrap"  VerticalAlignment="Top" Width="120"/>
        <Label Content="Or this lable" HorizontalAlignment="Left" Margin="161,42,0,0" VerticalAlignment="Top" MouseDown="lblSoure_MouseDown"/>
        <Label AllowDrop="True" Content="To this lable" HorizontalAlignment="Left" Margin="166,38,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.9,4.038" Grid.Row="1" Drop="Label_Drop"/>

    </Grid>
</Window>
View Code

 视频十二:控件类(控件类,背景画刷和前景画刷,字体,鼠标光标)

               RGB R:红色,G:绿色,B:蓝色。0-255

               使用两种代码方式来给button赋颜色:

               第一种:

               button1.Background =new SolidColorBrush(Colors.Red);////背景色
               button1.Foreground = System.Windows.SystemColors.ControlDarkBrush;///前景色

               第二种:
               button1.Background = new SolidColorBrush(Color.FromRgb(100,255,100));
               button1.Foreground = System.Windows.SystemColors.ControlDarkBrush;

               使用XAML直接在控件上显示:Background="颜色"

                <Button  Background="Blue" Name="button1" Content="Button"></Button>

               字体属性:FontFamily 字体的名称,如:宋体,楷体

                             FontSize     字体的大小

                             FontStyle    字体的类型:如斜体

                             FontWeight 字体的重量:如:加粗

                             FontStrech  字体的拉伸,及压缩

                             TextDecorations="类型"  下划线的类型

                  <Button   Foreground="Red" FontFamily="Times New Roman,Arial" FontSize="36" FontStyle="Italic" FontStretch="Condensed" Background="Blue" Name="button1" Content="Button"></Button>

                获取系统已经安装的字体:

                     foreach (FontFamily item in Fonts.SystemFontFamilies)
                     {
                           list.Items.Add(item.Source);
                     }

                    字体的添加:bayern.tff 首先添加到项目中,设置属性,生成操作为Resource.

                     FontFamily="./#bayern"      

                     文本渲染:附加属性  TextOptions.TextFormattingMode="Display" 文本内容更加清晰。(对于小字体效果更明显)

                     光标属性:继承框架元素类。

                      代码:this.Cursor=Cursors.Wait;

                      XAML:Cursor="Help"

视频十三:内容控件(Content属性,对齐内容,标签,按钮,工具提示)

              内容控件只能包含一个子元素控件,但是子元素控件里面可以包含多个子元素控件。

              Content 属性:Button控件是内容控件,所以只能包含一个子元素,但是如果子元素是面板容器,子元素内可以包含多个子元素。 对齐的方式:控件对齐 HorizontalAlignment="Left"  ;内容对齐 HorizontalContentAlignment="Center"  区分两种不同的对齐方式。对于image控件中的Source属性的使用还带学习。

              Label标签:Targer="" 属性

               <Label Name="label1" Target="{Binding ElementName=textBox1}">Choose _A</Label>  ----当按下ALT之后,才会出现快捷键方式,按下后,焦点会在txtBox1上面。
               <TextBox Name="textBox1" Margin="3">789789</TextBox>

              按钮:a. 属性 IsCancel="True"  ,则在键盘上按ESC则会触发这个按钮。

                       b. 属性IsDefault="True"  ,则在键盘上按Enter会触发这个按钮。

                       c. 使用CheckBox   

                               <CheckBox IsChecked="{x:Null}" IsThreeState="True"></CheckBox> ////未确定,且可以选择未确定状态
                               <CheckBox  IsChecked="True" IsThreeState="True“ ></CheckBox> ////选中状态,且可以选择未确定状态

                               <CheckBox  IsChecked="False" IsThreeState="False“ ></CheckBox> ////未选中状态,且只能选择选中跟未选中状态

                       d.使用RadioButton 若是在不同的容器中,则不会互相影响,若是在不同的容器中,想要互斥,则需要添加属性GroupName="rdo",设置相同的组。则会互斥。

                工具提示:  

                         简单提示:属性 ToolTip="This is my tooltip."    

                         复杂提示: 

                          <ToolTip Background="#60AA4030" > 使用RGB 加了60 则表示透明度。百分之60.

                           <ToolTip Background="#30AA4030" Placement="Mouse" HorizontalOffset="20" > 表示已鼠标为基准,便宜20像素的地方,出现提示。

                       

<Window x:Class="_2014_10_11_13内容控件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="584.965" Width="525">
    <Grid>
        <StackPanel Margin="0,0,0,0">
            <Button  IsDefault="True" IsCancel="True"  HorizontalAlignment="Left" HorizontalContentAlignment="Center"  Height="20" Name="Button1" Margin="3" Click="Button3_Click" >
                <Button.ToolTip>
                    <ToolTip Background="#30AA4030" Placement="Mouse" HorizontalOffset="20" >
                    <StackPanel>
                        <TextBlock>Image and text</TextBlock>
                        <Image Source="bin/123.jpg" ></Image>
                    </StackPanel>
                    </ToolTip>
                </Button.ToolTip>
                <Button.Content>Button1</Button.Content>
            </Button>            
            <Button ToolTip="This is my tooltip." IsDefault="True" IsCancel="True"  HorizontalAlignment="Left" HorizontalContentAlignment="Center" Content="Button" Height="20" Name="Button2" Margin="3" Click="Button3_Click" />
            <Button   Height="100" Name="Button3" Margin="3">
                <Image Source="bin/123.jpg" ></Image></Button>
            <Button Height="auto" Name="Button4" Margin="3" >
                <StackPanel>
                    <TextBlock>Image and text button</TextBlock>
                    <Image Source="bin/123.jpg"  Stretch="None"></Image>
                    <TextBlock>Image and text button</TextBlock>
                </StackPanel>
            </Button>
            <Label Name="label1" Target="{Binding ElementName=textBox1}">Choose _A</Label>
            <TextBox Name="textBox1" Margin="3">789789</TextBox>
            <CheckBox IsChecked="{x:Null}" IsThreeState="True"></CheckBox>
            <CheckBox  IsChecked="True"></CheckBox>
        </StackPanel>
    </Grid>
</Window>
View Code

                         Popup控件:需要自己手动添加,与ToolTip 的功能类是。
 

<Window x:Class="_2014_10_11_13内容控件.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>
        <TextBlock TextWrapping="Wrap">
            You can use a Popup to provide a link for a specific 
            <Run TextDecorations="UnderLine" MouseEnter="Run_MouseEnter">
            term     
            </Run>of interest.
        </TextBlock>
        <Popup Name="popLink" StaysOpen="False" Placement="Mouse" MaxWidth="200"
               PopupAnimation="Slide" AllowsTransparency="True">
            <Border BorderBrush="Beige" BorderThickness="2" Background="White">
                <TextBlock Margin="10" TextWrapping="Wrap">
                    For more informtion,see
                    <Hyperlink Name="lnk" NavigateUri="http://www.baidu.com" Click="Hyperlink_Click">Wikipedia</Hyperlink>
                </TextBlock>
            </Border>
            
        </Popup>
    </Grid>
</Window>
View Code
using System;
using System.Collections.Generic;
using System.Diagnostics;
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.Shapes;

namespace _2014_10_11_13内容控件
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Run_MouseEnter(object sender, MouseEventArgs e)
        {
            popLink.IsOpen = true;
        }

        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            Process.Start(((Hyperlink)sender).NavigateUri.ToString());
        }
    }
}
View Code

视频十四:特殊容器控件(滚动控件,ScrollViewer)

               系统设置及系统提供的方法:滚动到下一个元素  CanContentScroll="True"

using System;
using System.Collections.Generic;
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 _2014_10_12_13特殊容器控件
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //this.scrollViewer.LineUp();向上一行
            //this.scrollViewer.LineDown();向下一行
            //this.scrollViewer.PageUp();向上一页
            //this.scrollViewer.PageDown();//向下一页
            //this.scrollViewer.ScrollToHome();//置顶
            //this.scrollViewer.ScrollToEnd();//置底
            ///自定义滚动
            
        }
    }
}
View Code
<Window x:Class="_2014_10_12_13特殊容器控件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="290.299" Width="471.269">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <ScrollViewer Grid.Row="1" Margin="10" Name="scrollViewer" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
            <TextBox xml:space="preserve"  HorizontalAlignment="Left" Height="110" TextWrapping="Wrap"  VerticalAlignment="Top" Width="443" RenderTransformOrigin="0.497,0.405">
                Andrew博客园
                Andrew博客园
                Andrew博客园
                Andrew博客园
                Andrew博客园
                Andrew博客园
                Andrew博客园
                Andrew博客园
                Andrew博客园
            </TextBox>

        </ScrollViewer>
        <Button Content="lineUp" Name="button1" Click="button1_Click">
        </Button>
    </Grid>
</Window>
View Code

视频十五:带标题的内容控件(GroupBox,TabItem,Expander 三个类) 

              GroupBox:最简单的控件

             

<Window x:Class="_2014_10_13_15带标题的内容控件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <GroupBox Header="123" Margin="5" Name="groupBox1">
            <StackPanel>
                <RadioButton Content="One" Height="16" Name="radioButton1"></RadioButton>
                <RadioButton Content="Two" Height="16" Name="radioButton2"></RadioButton>
                <RadioButton Content="Three" Height="16" Name="radioButton3"></RadioButton>
                <RadioButton Content="Four" Height="16" Name="radioButton4"></RadioButton>
                <Button Content="Save" Height="23" Name="button1" Margin="3"></Button>
            </StackPanel>
        </GroupBox>
    </Grid>
</Window>
View Code


             TabItem:分页控件容器

<Window x:Class="_2014_10_13_15带标题的内容控件.tabItem"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="tabItem" Height="300" Width="300">
    <Grid>
        <TabControl Margin="3" Name="tabControl1" TabStripPlacement="Left">
            <TabItem Header="001" Name="tabItem1">
                <StackPanel Name="stackPanel" Margin="3">
                    <CheckBox Name="checkBox1" Margin="3">Setting One</CheckBox>
                    <CheckBox Name="checkBox2" Margin="3">Setting Two</CheckBox>
                    <CheckBox Name="checkBox3" Margin="3">Setting Three</CheckBox>
                    <Button Content="Button" Name="Button1" Click="Button1_Click"/>
                </StackPanel>
            </TabItem>
            <TabItem  Name="tabItem2">
                <TabItem.Header>
                    <StackPanel>
                        <TextBlock Margin="3">Image and Text Tab Title</TextBlock>
                        <Image Source="/bin/123.jpg" Stretch="None" Height="20" Width="150"></Image>
                    </StackPanel>
                </TabItem.Header>
            </TabItem>
        </TabControl>
    </Grid>
</Window>
View Code
using System;
using System.Collections.Generic;
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.Shapes;

namespace _2014_10_13_15带标题的内容控件
{
    /// <summary>
    /// tabItem.xaml 的交互逻辑
    /// </summary>
    public partial class tabItem : Window
    {
        public tabItem()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            this.tabItem2.IsSelected = true;
        }
    }
}
View Code

              Expander 控件,隐藏部分数据的控件

  

<Window x:Class="_2014_10_13_15带标题的内容控件.expander"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="expander" Height="300" Width="300" SizeToContent="Height">
    <StackPanel Name="stackPanel1" Margin="3" >
        <Expander Name="expander1" Height="100" Margin="5" Padding="5" ExpandDirection="Left">
            <Button Name="Button1"></Button>
        </Expander>
        <Expander Name="expander2"   Margin="5" Padding="5">
            <ScrollViewer Name="scrollViewer" Margin="3">
                <TextBlock TextWrapping="Wrap" >内容内容内容</TextBlock>
            </ScrollViewer>           
        </Expander>
       
    </StackPanel>
</Window>
View Code

视频十六:文本控件(多行文本,选择文本,拼写检查,PasswordBox)(TextBox,RichTextBox,PasswordBox)

              多行文本:txtbox属性,有多行自动换行 TextWrapping="Wrap";有滚动条属性,VerticalScrollBarVisibility="Visible",也有LineUp(),LineDown()等滚动条属性的方法。

              选择文本:txtbox属性,SelectionStart属性(从0开始),SelectionLength(字符长度),SelectedText(选中的属性)。

              拼写检查:txtbox属性,SpellCheck.IsEnabled="True"  仅支持英文输入法键盘设置。

              PasswordBox:密码框控件,PasswordChar="格式"  不支持复制粘帖,纯文本对象。

   

<Window x:Class="_2014_10_13_16文本控件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox SpellCheck.IsEnabled="True"  TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" Name="textBox1"  Margin="3,3,3,24.053" MaxLength="50000"  Text="Label和TextBlock都是System.Windows.Controls命名空间下的类,但二者的父类并不相同。TextBlock继承自System.Windows.FrameworkElement,从这个角度讲,TextBlock不能称之为控件(因为它没有继承Control类,关于Control类,我会在WPF Unleashed第四章为大家介绍),而Label继承自System.Windows.ContentControl。FrameworkElement是非常底层的类,它同时也是ContentControl的父类。所以,Label相对TextBlock更加高级一些,它能够完成TextBlock所无法完成的工作。例如对于Access key的支持,而且我们可以在Label内可以放置任意对象,而TextBlock只能显示文本。" SelectionChanged="textBox1_SelectionChanged" ></TextBox>
        <ScrollViewer Foreground="blue" Grid.Row="1" Margin="0,10,0,5" VerticalScrollBarVisibility="Auto">
            <StackPanel>
                <TextBlock>Current Selection: </TextBlock>
                <TextBlock Name="txtSelection" TextWrapping="Wrap"></TextBlock>
            </StackPanel>
        </ScrollViewer>
        <PasswordBox Grid.Row="2" PasswordChar="8" Height="30" Width="300" ></PasswordBox>
    </Grid>
</Window>
View Code
using System;
using System.Collections.Generic;
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 _2014_10_13_16文本控件
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void textBox1_SelectionChanged(object sender, RoutedEventArgs e)
        {
            if (this.txtSelection == null) return;
            this.txtSelection.Text = "Selection from"+this.textBox1.SelectionStart+" to "+this.textBox1.SelectionLength
                + "  is   " +this.textBox1.SelectedText;
        }
    }
}
View Code

视频十七:列表控件(ListBox,ComboBox 下拉列表) 

               Listbox:控件里面不一定要ListboxItem,直接添加StackPanel也是可以的。                  

<Window x:Class="_2014_10_13_17列表控件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox Height="311.842" Margin="5,5,5,0" Name="listBox1" VerticalAlignment="Top">
            <ListBoxItem>
                <Image Height="40" Source="/bin/123.jpg"></Image>
            </ListBoxItem>
            <ListBoxItem>Blue</ListBoxItem>
            <ListBoxItem>Yellow</ListBoxItem>
            <ListBoxItem>Red</ListBoxItem>
            <ListBoxItem>Green</ListBoxItem>
            <ListBoxItem>Blue</ListBoxItem>
            <ListBoxItem>Yellow</ListBoxItem>
            <ListBoxItem>Red</ListBoxItem>
            <StackPanel Orientation="Horizontal">
                <Image Height="40" Source="/bin/123.jpg"></Image>
                <Label VerticalContentAlignment="Center">A Happy Face</Label>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <Image Height="40" Source="/bin/123.jpg"></Image>
                <Label VerticalContentAlignment="Center">Two Happy Face</Label>
            </StackPanel>
            <StackPanel Orientation="Horizontal"></StackPanel>
        </ListBox>
    </Grid>
</Window>
View Code

               在Listbox中,添加对象,并获取选中项

<Window x:Class="_2014_10_13_16列表控件.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 Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions> 
        <ListBox Name="lst" Height="auto" Margin="5" SelectionChanged="lst_SelectionChanged">
            <CheckBox Margin="3">Option 1</CheckBox>
            <CheckBox Margin="3">Option 2</CheckBox>
            <CheckBox Margin="3">Option 3</CheckBox>
        </ListBox>
        <StackPanel Grid.Row="1" Margin="0,10,0,0"> 
            <TextBlock>Current selection:</TextBlock>
            <TextBlock Name="txtSelection" TextWrapping="Wrap"></TextBlock>
            <Button Margin="0,10,0,0" Click="Button_Click">Examine All Items </Button>
        </StackPanel>
    </Grid>
</Window>
View Code
using System;
using System.Collections.Generic;
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.Shapes;

namespace _2014_10_13_16列表控件
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void lst_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lst.SelectedItem == null) return;
            this.txtSelection.Text = "You chose item at position " + (lst.SelectedIndex+1)
                +"	
 Checked state is "+((CheckBox)lst.SelectedItem).IsChecked;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (CheckBox item in lst.Items)
            {
                if (item.IsChecked == true)
                {
                    sb.Append(item.Content);
                    sb.Append(" is checked.");
                    sb.Append("	
");
                }
               
            }
            txtSelection.Text = sb.ToString();
        }
    }
}
View Code

视频十八:基于范围的控件(Slider 滑动条控件,ProgressBar 进度条控件) 基于最大值与最小值之间。    

              Slider 控件:类似于,播放声音的,数值没有那么准确的重要性。           

                                Ticks="10,30,70"  不规则划分 滑动条

                                特定的部分设置为,提示部分80-100时候:    IsSelectionRangeEnabled="True"  SelectionStart="80" SelectionEnd="100" 

                                设置滑动的距离: 最小值 SmallChange="1"  最大值  LargeChange="5"

             ProgressBar控件:绿色脉冲 自动滚动:IsIndeterminate="True"

<Window x:Class="_2014_10_13_18基于范围的控件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Slider SmallChange="1" LargeChange="5" IsSelectionRangeEnabled="True"  SelectionStart="70" SelectionEnd="100"  Maximum="100" Minimum="0" Value="2"  TickFrequency="5" TickPlacement="BottomRight"    Name="silder1" Margin="5" VerticalAlignment="Top" Height="auto"></Slider>
        <ProgressBar Name="progressBar" Maximum="100" Minimum="0" Margin="-0.264,129.789,5.264,118.21" Grid.Row="1" Height="40" IsIndeterminate="True"></ProgressBar>
        <Button HorizontalAlignment="Left" Grid.Row="1" Width="100" Margin="194.736,212.421,0,-4.684" Click="Button_Click"/>
    </Grid>
</Window>
View Code
using System;
using System.Collections.Generic;
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 _2014_10_13_18基于范围的控件
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.progressBar.Value += 2;
        }
    }
}
View Code

视频十九:日期控件(Calendar控件,DatePicker控件)

                Calendar 日历控件:   <Calendar DisplayMode="Month">默认情况下是模式是Month</Calendar>

<Window x:Class="_2014_10_13_19日期控件.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Calendar IsTodayHighlighted="True" SelectionMode="MultipleRange" Name="calendar" DisplayMode="Month" DisplayDateStart="2014-10-1" DisplayDateEnd="2014-10-31" FirstDayOfWeek="Monday" SelectedDatesChanged="Calendar_SelectedDatesChanged"></Calendar>
        <Button Content="Button" HorizontalAlignment="Left" Margin="208,216,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>
View Code
using System;
using System.Collections.Generic;
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 _2014_10_13_19日期控件
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
        {
            //this.Title = this.calendar.SelectedDate.ToString();
            //this.Title = this.calendar.SelectedDates.ToString();////存多个时期,是一个集合
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string s = "";
            for (int i = 0; i < this.calendar.SelectedDates.Count; i++)
            {
                s += this.calendar.SelectedDates[i].ToShortDateString() + "  ";
            }
            this.Title=s;
        }
    }
}
View Code

               DatePicker控件:

               <DatePicker Height="25" Width="100" IsDropDownOpen="False" Margin="198,183,219,112" DateValidationError="DatePicker_DateValidationError"  ></DatePicker>

               private void DatePicker_DateValidationError(object sender, DatePickerDateValidationErrorEventArgs e)
               {
                       ////用户输入非法的数据
                       MessageBox.Show(e.Text+"  is not a valid value beause."+e.Exception.Message);
               }

                                              

视频二十:Application类

              创建Application对象,其实App.xaml+App.xaml.cs 俩个加起来的功能与如下的代码是一样的效果。

              

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;

namespace _2014_10_13_20Application类
{
    class Startup
    {
        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            MainWindow win = new MainWindow();
            app.Run(win);
        }
    }
}
View Code

               派生一个自定义的Application类 :App.xaml.cs 里面的App类就是了。
               应用程序的关闭方式:ShutdownMode 关闭模式。

                StartupUri="MainWindow.xaml" ShutdownMode="OnLastWindowClose">   ////默认模式,可以不写

               应用程序事件: Application 的事件,有两种方法,一种是单纯的直接使用事件,第二种是重写受保护的方法。

          

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Windows;

namespace _2014_10_13_20Application类
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {

        }
        ///重写受保护的方法
        protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
        {
            base.OnSessionEnding(e);
            e.Cancel = true;////阻止关闭系统
            MessageBox.Show("无法注销或关闭系统");
        }
    }
}
View Code
<Application x:Class="_2014_10_13_20Application类.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml" ShutdownMode="OnLastWindowClose" DispatcherUnhandledException="Application_DispatcherUnhandledException">
    <Application.Resources>
         
    </Application.Resources>
</Application>
View Code

视频二十一:Application 类的任务(显示初始界面,处理命令行参数,访问当前Application对象,在窗口之间进行交互)

                 显示初始页面:添加一张图片,设置属性,生成操作为SplashScreen,则在窗口第一次运行的时候,则会显示该图片(300毫秒)。

                 处理命令行参数:加载TextFile.txt文档。

                 首先添加TextFile.txt文档到bin目录下,设置文档的属性:始终复制。

                 在App.xaml将 StartupUri="MainWindow.xaml"替换成Startup="Application_Startup"事件方法

                 在项目的属性,调试的命令行参数填写文档的名称:TextFile.txt

    <Application x:Class="_2014_10_14_21Application任务.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Startup="Application_Startup"
             >
    <Application.Resources>
         
    </Application.Resources>
</Application>
View Code
////App.xaml.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Windows;

namespace _2014_10_14_21Application任务
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            MainWindow win = new MainWindow();
            if (e.Args.Length > 0)
            {
                string file=e.Args[0];
                if(File.Exists(file))
                {
                    win.LoadFile(file);
                }
            }
            win.Show();
        }
    }
}
View Code
using System;
using System.Collections.Generic;
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 _2014_10_14_21Application任务
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public void LoadFile(string path)
        {
            this.Content = System.IO.File.ReadAllText(path);
            this.Title = path;
        }
    }
}
View Code

                 访问当前Application主窗口的对象: Application.Current.MainWindow

                       一次启动俩个窗口, Application.Current.MainWindow来辨别主窗口。

                       windows集合: 

                        private void Button_Click(object sender, RoutedEventArgs e)
                          {
                                ///MessageBox.Show("The main window is "+ Application.Current.MainWindow.Title);
                               foreach (Window item in Application.Current.Windows)////Windows集合
                               {
                                  MessageBox.Show(item.Title+" is opened.");
                               }
                          }

WPF视频教程系列笔记                  在窗口之间进行交互:

  

////App.xaml.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Windows;

namespace _2014_10_14_21Application任务
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        //private void Application_Startup(object sender, StartupEventArgs e)
        //{
        //    MainWindow win = new MainWindow();
        //    if (e.Args.Length > 0)
        //    {
        //        string file=e.Args[0];
        //        if(File.Exists(file))
        //        {
        //            win.LoadFile(file);
        //        }
        //    }
        //    win.Show();
        //}

        private List<Document> documents = new List<Document>();

        public List<Document> Documents
        {
            get { return documents; }
            set { documents = value; }
        }
    }
}
View Code
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.Shapes;

namespace _2014_10_14_21Application任务
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ///MessageBox.Show("The main window is "+ Application.Current.MainWindow.Title);
            //foreach (Window item in Application.Current.Windows)////Windows集合
            //{
            //    MessageBox.Show(item.Title+" is opened.");
            //}
            Document doc = new Document();
            doc.Owner = this;
            doc.Show();
            ((App)Application.Current).Documents.Add(doc);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            foreach (Document item in ((App)Application.Current).Documents)
            {
                item.Content = "Refreshed at " + DateTime.Now.ToLongTimeString() + ".";
                MessageBox.Show(item.Title + " is opened.");
            }
        }
    }
}
View Code

 视频二十二:单实例应用程序(创建单实例应用程序包装器) office word是单实例,文本txt是多实例。         

                  实现单实例应用程序:

  

//WpfApp.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace _2014_10_14_22单实例应用程序
{
    class WpfApp:System.Windows.Application
    {
        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
            base.OnStartup(e);
            showWindow();
        }
        public void showWindow()
        {
            MainWindow win = new MainWindow();
            win.Show();
        }
    }
}
View Code
using System;
//Startup.cs
using System.Collections.Generic;
using System.Text;

namespace _2014_10_14_22单实例应用程序
{
    class Startup
    {
        [STAThread]
        public static void Main(string[] args)
        {
            //WpfApp app = new WpfApp();
            //app.Run();
            SingleInstanceApplicationWrapper wrapper = new SingleInstanceApplicationWrapper();
            wrapper.Run(args);
        }
     
    }
}
View Code
////SingleInstanceApplicationWrapper.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace _2014_10_14_22单实例应用程序
{
    class SingleInstanceApplicationWrapper:Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
        public SingleInstanceApplicationWrapper()
        {
            this.IsSingleInstance = true;
        }
        private WpfApp app;
        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
        {
            //return base.OnStartup(eventArgs);
            app = new WpfApp();
            app.Run();
            return false;
        }
        protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs eventArgs)
        {
            base.OnStartupNextInstance(eventArgs);
            app.showWindow();
        }
    }
}
View Code

视频二十三:程序集资源,二进制资源(添加资源,内容文件)

                  举例说明:添加图片文件

                  添加资源:默认情况下,设置图片文件的属性,复制到输出目录:不复制,生成操作:Resource

                  内容文件:设置图片文件的属性,复制到输出目录:始终复制,生成操作:内容

  

视频二十四:将元素绑定到一起(绑定表达式,绑定错误,绑定模式,使用代码创建绑定多绑定,绑定更新)

                例子:FontSize="{Binding ElementName=silder1,Path=Value,Mode=TwoWay}

                 绑定表达式:FontSize="{Binding ElementName=silder1,Path=Value,Mode=TwoWay}

                 绑定错误:属性错误的时候,不会提示错误,只是没有任何改变罢了。

                 绑定模式:Mode=TwoWay 双向改变

                 使用代码创建绑定多绑定:

                private void Window_Loaded(object sender, RoutedEventArgs e)
                {
                Binding binding = new Binding();
                binding.Source = this.silder1;
                binding.Path = new PropertyPath("Value");
               binding.Mode = BindingMode.TwoWay;
               this.textBlock2.SetBinding(TextBlock.FontSizeProperty,binding);
               } 

               取消绑定: 

                private void Button_Click_1(object sender, RoutedEventArgs e)
                 {
               BindingOperations.ClearAllBindings(this.textBlock2);
                  }

                  绑定立即更新:UpdateSourceTrigger=PropertyChanged

                     <TextBox Margin="3" Height=" 23" Name="textBox1" Width="120" Text="{Binding                                 ElementName=textBlock2,Path=FontSize,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>

   

<Window x:Class="_2014_10_18_24将元素绑定到一起.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>

        <StackPanel Margin="5" Name="stackPanel1">
            <Slider Height="auto" Name="silder1" Margin="3" Minimum="1" Maximum="40" Value="10" TickPlacement="TopLeft" TickFrequency="1" IsSnapToTickEnabled="True"></Slider>
            <TextBlock Name="textBlock1" Text="Simple Text" Margin="10" FontSize="{Binding ElementName=silder1,Path=Value,Mode=TwoWay}"/>
            <TextBlock Name="textBlock2" Text="Simple Text" Margin="10" Foreground="{Binding ElementName=listBox1,Path=SelectedItem.Tag}"/>
            <Button Content="Button" Margin="5" Width="75" Click="Button_Click"/>
            <Button  Content="Cancel Binding" Margin="216,5" Width="75" Click="Button_Click_1"/>
            <ListBox Margin="10" Height="100" Name="listBox1">
                <ListBoxItem Tag="Blue">Blue</ListBoxItem>
                <ListBoxItem Tag="DarkBlue">Blue</ListBoxItem>
                <ListBoxItem Tag="lightBlue">light Blue</ListBoxItem>
            </ListBox>
            <TextBox Margin="3" Height=" 23" Name="textBox1" Width="120" Text="{Binding ElementName=textBlock2,Path=FontSize,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
        </StackPanel>
 
    </Grid>
</Window>
View Code
using System;
using System.Collections.Generic;
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 _2014_10_18_24将元素绑定到一起
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //this.silder1.Value = 30;
            this.textBlock1.FontSize = 30;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Binding binding = new Binding();
            binding.Source = this.silder1;
            binding.Path = new PropertyPath("Value");
            binding.Mode = BindingMode.TwoWay;
            this.textBlock2.SetBinding(TextBlock.FontSizeProperty,binding);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            BindingOperations.ClearAllBindings(this.textBlock2);
        }
    }
}
View Code

视频二十五:绑定到非元素对象(Source属性,RelativeSource属性,DataContext属性)

                 Source属性:

<Window x:Class="_2014_10_18_25绑定到非元素对象.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources> 
        <FontFamily x:Key="CustomFont">Calibri</FontFamily>
        <FontFamily x:Key="CustomFont1">Calibri1</FontFamily>
    </Window.Resources>
    <Grid>
        <StackPanel Margin="5" Name="stackPanel1">
            <TextBlock Name="textBlock1" Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=Source}"></TextBlock>
            <TextBlock Name="textBlock2" Text="{Binding Source={StaticResource CustomFont},Path=Source}"></TextBlock>
        </StackPanel>
    </Grid>
</Window>
View Code

                 RelativeSource属性:

                  <RelativeSource Mode="FindAncestor">

<Window x:Class="_2014_10_18_25绑定到非元素对象.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources> 
        <FontFamily x:Key="CustomFont">Calibri</FontFamily>
        <FontFamily x:Key="CustomFont1">Calibri1</FontFamily>
    </Window.Resources>
    <Grid>
        <StackPanel Name="StackPaenlOut">
        <StackPanel Margin="5" Name="stackPanel1">
            <TextBlock Name="textBlock1" Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=Source}"></TextBlock>
            <TextBlock Name="textBlock2" Text="{Binding Source={StaticResource CustomFont},Path=Source}"></TextBlock>
            <TextBlock Name="textBlock3" Margin="5">
                <TextBlock.Text>
                    <Binding Path="Title">
                        <Binding.RelativeSource>
                                <RelativeSource Mode="FindAncestor" AncestorType="{x:Type Window }"></RelativeSource>
                        </Binding.RelativeSource>
                    </Binding>
                </TextBlock.Text>
            </TextBlock>
                <TextBlock Name="textBlock4" Margin="5" Text="{Binding Path=Name,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type StackPanel},AncestorLevel=2}}">              
            </TextBlock>
        </StackPanel>
        </StackPanel>
    </Grid>
</Window>
View Code

                DataContext属性:DataContext="{x:Static SystemFonts.IconFontFamily} 添加在上一层的元素属性中,可以减少子元素多次使用的,相当于统一声明功能。

      

<Window x:Class="_2014_10_18_25绑定到非元素对象.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources> 
        <FontFamily x:Key="CustomFont">Calibri</FontFamily>
        <FontFamily x:Key="CustomFont1">Calibri1</FontFamily>
    </Window.Resources>
    <Grid>
        <StackPanel Name="StackPaenlOut">
            <StackPanel Margin="5" Name="stackPanel1" DataContext="{x:Static SystemFonts.IconFontFamily}">
            <TextBlock Name="textBlock1" Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=Source}"></TextBlock>
            <TextBlock Name="textBlock2" Text="{Binding Source={StaticResource CustomFont},Path=Source}"></TextBlock>
            <TextBlock Name="textBlock3" Margin="5">
                <TextBlock.Text>
                    <Binding Path="Title">
                        <Binding.RelativeSource>
                                <RelativeSource Mode="FindAncestor" AncestorType="{x:Type Window }"></RelativeSource>
                        </Binding.RelativeSource>
                    </Binding>
                </TextBlock.Text>
            </TextBlock>
                <TextBlock Name="textBlock4" Margin="5" Text="{Binding Path=Name,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type StackPanel},AncestorLevel=2}}">              
            </TextBlock>
                <TextBlock Name="textBlock5" Text="{Binding Path=Source}"></TextBlock>
                <TextBlock Name="textBlock6" Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=LineSpacing}"></TextBlock>
                <TextBlock Name="textBlock7" Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=FamilyTypefaces[0].Style}"></TextBlock>
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>
View Code

      

 视频二十六:资源基础(WPF资源,资源集合,资源层次,静态资源和动态资源,通过代码访问,应用程序资源,系统资源)

                 资源定义在引用之前定义。

               

<Window x:Class="_2014_10_21_26资源基础.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ImageBrush x:Key="TitleBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32" ImageSource="bin 01.jpg" ></ImageBrush>
    </Window.Resources>
    <StackPanel Name="stackPanel1" Margin="3">
        <Button Name="button1" Background="{StaticResource TitleBrush}" Margin="3" Content="A Tiled Button" FontSize="14" Padding="5"></Button>
        <Button Name="button2" Margin="3" Content="Button" FontSize="14" Padding="5">
            <Button.Resources>
                <ImageBrush x:Key="TitleBrush" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32" ImageSource="bin 01.jpg" ></ImageBrush>
            </Button.Resources>
            <Button.Background>
                <StaticResource ResourceKey="TitleBrush"></StaticResource>
            </Button.Background>
        </Button>
        
        <Button Name="button3" Margin="3"  Background="{DynamicResource TitleBrush}" Content="Button" FontSize="14" Padding="5"></Button>
    </StackPanel>
</Window>
View Code

视频二十七:资源字典(创建资源字典,使用资源字典,在程序集之间共享资源)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ImageBrush x:Key="TileBrush1" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32" Opacity="0.3" ImageSource="Debug 01.jpg"></ImageBrush>
    <ImageBrush x:Key="TileBrush2" TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32" Opacity="0.3" ImageSource="Debug 01.jpg"></ImageBrush>
</ResourceDictionary> 
View Code
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
</ResourceDictionary>
View Code
<Window x:Class="_2014_11_03_27资源字典.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <StackPanel Name="stackPanel1" Margin="5">
        <Button Content="Button" Name="button1" Margin="5" Padding="5" FontSize="14" Background="{DynamicResource TileBrush1}"></Button>
        <Button Content="Button" Name="button2" Margin="5" Padding="5" FontSize="14" Background="{DynamicResource TileBrush2}"></Button>
        <Button Content="Button" Name="button3" Margin="5" Padding="5" FontSize="14"></Button>
    </StackPanel>
</Window>
View Code
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 _2014_11_03_27资源字典
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {   ////通过编译类库,添加引用ResourceLibray,则使用代码添加使用资源
            ResourceDictionary rd = new ResourceDictionary();
            rd.Source = new Uri("ResourceLibray;component/ResourceDictionary1.xaml",UriKind.Relative);
            this.button3.Background = (Brush)rd["TitleBrush1"];
            ////
        }
    }
}
View Code

 视频二十八:样式基础类似于CSS(样式基础,创建样式,设置属性,关联事件处理程序,多层样式,通过类型自动应用样式)

                   首先是传统的方式,不仅没有简化,而且还增加了复杂度。

                  

<Window x:Class="_2014_11_03_28样式基础.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525" >
    <Window.Resources>
        <FontFamily x:Key="ButtonFontFamily" >Times New Roman</FontFamily>
        <sys:Double x:Key="ButtonFontSize">18</sys:Double>
        <FontWeight x:Key="ButtonFontWeight">Bold</FontWeight>
    </Window.Resources>
    <StackPanel Name="stackPanel1" Margin="5">
        <Button  Name="button1" Content="A Customaized Button" Margin="5" Padding="5"
                 FontFamily="{StaticResource ButtonFontFamily}" FontSize="{StaticResource ButtonFontSize}" FontWeight="{StaticResource ButtonFontWeight}"></Button>
        <TextBlock Margin="5">Noraml Conent</TextBlock>
        <Button Margin="5" Padding="5">A normal Button</Button>
        <TextBlock Margin="5">More Noraml Content</TextBlock>
        <Button Margin="5" Padding="5"  FontFamily="{StaticResource ButtonFontFamily}" FontSize="{StaticResource ButtonFontSize}" FontWeight="{StaticResource ButtonFontWeight}">Another Customized Button</Button>
    </StackPanel>
</Window>
View Code


                   通过创建样式:

<Window x:Class="_2014_11_03_28样式基础.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525" >
    <Window.Resources>
        <FontFamily x:Key="ButtonFontFamily" >Times New Roman</FontFamily>
        <sys:Double x:Key="ButtonFontSize">18</sys:Double>
        <FontWeight x:Key="ButtonFontWeight">Bold</FontWeight>
        <Style x:Key="BigFontButtonStyle" >
            <Setter Property="Control.FontFamily" Value="Times New Roman"></Setter>
            <!--<Setter Property="Control.FontSize" Value="18"></Setter>-->
            <!--<Setter Property="Button.FontSize" Value="18"></Setter>
            从中可以看到虽然是分别设置了属性,但是只以最后设置的属性为准。
            <Setter Property="TextBlock.FontSize" Value="10"></Setter>-->
            <Setter Property="Control.FontWeight" Value="Bold"></Setter>
            <Setter Property="Control.Background">
                <Setter.Value>
                    <ImageBrush TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32" Opacity="0.3" ImageSource="bin 01.jpg"></ImageBrush>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <StackPanel Name="stackPanel1" Margin="5">
        <Button  Name="button1" Content="A Customaized Button" Margin="5" Padding="5"
                 FontFamily="{StaticResource ButtonFontFamily}" FontSize="{StaticResource ButtonFontSize}" FontWeight="{StaticResource ButtonFontWeight}"></Button>
        <TextBlock Margin="5">Noraml Conent</TextBlock>
        <Button Margin="5" Padding="5">A normal Button</Button>
        <TextBlock Margin="5" Padding="5"  Style="{StaticResource BigFontButtonStyle}">More Noraml Content</TextBlock>
        <Button Margin="5" Padding="5"  FontFamily="{StaticResource ButtonFontFamily}" FontSize="{StaticResource ButtonFontSize}" FontWeight="{StaticResource ButtonFontWeight}">Another Customized Button</Button>
        <Button Margin="5" Padding="5"  Style="{StaticResource BigFontButtonStyle}">Another Customized Button</Button>
    </StackPanel>
</Window>
View Code

                  关联事件处理程序

<Window x:Class="_2014_11_03_28样式基础.EventSetter"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="EventSetter" Height="300" Width="300">
    <Window.Resources>
        <Style x:Key="MouseOverHighLight">
            <Setter Property="TextBlock.Padding" Value="5"></Setter>
            <EventSetter Event="FrameworkElement.MouseEnter" Handler="element_MouseEnter"></EventSetter>
            <EventSetter Event="FrameworkElement.MouseLeave" Handler="element_MouseLeave"></EventSetter>
        </Style>
    </Window.Resources>
    <StackPanel Name="stackPanel">
        <TextBlock Style="{StaticResource MouseOverHighLight}">Hover over me.</TextBlock>
        <TextBlock Padding="5">Don't bother with me.</TextBlock>
        <TextBlock Style="{StaticResource MouseOverHighLight}">Hover over me.</TextBlock>
    </StackPanel>
</Window>
View Code
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.Shapes;

namespace _2014_11_03_28样式基础
{
    /// <summary>
    /// EventSetter.xaml 的交互逻辑
    /// </summary>
    public partial class EventSetter : Window
    {
        public EventSetter()
        {
            InitializeComponent();
        }
        private void element_MouseEnter(object sender,MouseEventArgs e)
        {
            ((TextBlock)sender).Background = new SolidColorBrush(Colors.LightBlue);
              
        }
        private void element_MouseLeave(object sender, MouseEventArgs e)
        {
            ((TextBlock)sender).Background = null;
        }
    }
}
View Code

 通过类型自动应用样式

<Window x:Class="_2014_11_03_28样式基础.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525" >
    <Window.Resources>
        <FontFamily x:Key="ButtonFontFamily" >Times New Roman</FontFamily>
        <sys:Double x:Key="ButtonFontSize">18</sys:Double>
        <FontWeight x:Key="ButtonFontWeight">Bold</FontWeight>
        <Style x:Key="BigFontButtonStyle" >
            <Setter Property="Control.FontFamily" Value="Times New Roman"></Setter>
            <!--<Setter Property="Control.FontSize" Value="18"></Setter>-->
            <!--<Setter Property="Button.FontSize" Value="18"></Setter>
            从中可以看到虽然是分别设置了属性,但是只以最后设置的属性为准。
            <Setter Property="TextBlock.FontSize" Value="10"></Setter>-->
            <Setter Property="Control.FontWeight" Value="Bold"></Setter>
            <Setter Property="Control.Background">
                <Setter.Value>
                    <ImageBrush TileMode="Tile" ViewportUnits="Absolute" Viewport="0 0 32 32" Opacity="0.3" ImageSource="bin 01.jpg"></ImageBrush>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="EmphasizeBigFontButtonStyle" BasedOn="{StaticResource BigFontButtonStyle}"> <!--BasedOn继承了上一个style-->            
            <Setter Property="Control.Foreground" Value="White"></Setter>
            <Setter Property="Control.Background" Value="LightBlue"></Setter>
        </Style>
        <Style  TargetType="Button"  BasedOn="{StaticResource BigFontButtonStyle}">
            <!--全部的Button使用这个样式,使用Style="{x:Null}",则不启用-->
            <Setter Property="Control.Foreground" Value="White"></Setter>
            <Setter Property="Control.Background" Value="LightBlue"></Setter>
        </Style>
    </Window.Resources>
    <StackPanel Name="stackPanel1" Margin="5">
        <Button  Name="button1" Content="A Customaized Button" Margin="5" Padding="5" Style="{x:Null}"
                 FontFamily="{StaticResource ButtonFontFamily}" FontSize="{StaticResource ButtonFontSize}" FontWeight="{StaticResource ButtonFontWeight}"></Button>
        <TextBlock Margin="5">Noraml Conent</TextBlock>
        <Button Margin="5" Padding="5">A normal Button</Button>
        <TextBlock Margin="5" Padding="5"  Style="{StaticResource BigFontButtonStyle}">More Noraml Content</TextBlock>
        <Button Margin="5" Padding="5"  FontFamily="{StaticResource ButtonFontFamily}" FontSize="{StaticResource ButtonFontSize}" FontWeight="{StaticResource ButtonFontWeight}">Another Customized Button</Button>
        <Button Margin="5" Padding="5"  Style="{StaticResource EmphasizeBigFontButtonStyle}">Another Customized Button</Button>
    </StackPanel>
</Window>
View Code

视频二十九:触发器(简单触发器,事件触发器)

简单触发器

<Window x:Class="_2014_11_03_29触发器.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="BigFontButton" >
            <Style.Setters>
                <Setter Property="Control.FontFamily" Value="Times New Roman"></Setter>
                <Setter Property="Control.FontSize" Value="32"></Setter>
            </Style.Setters>
            <Style.Triggers>               
                <!--<Trigger  Property="Button.IsPressed" Value="True">
                    <Setter Property="Control.Foreground" Value="Blue"></Setter>
                </Trigger>
                <Trigger  Property="Control.IsFocused" Value="True">
                    <Setter Property="Control.Foreground" Value="DarkRed"></Setter>
                </Trigger>-->
                <MultiTrigger>
                    <MultiTrigger.Conditions><!--表示多个条件为真的时候,才触发事件-->
                        <Condition Property="Control.IsFocused" Value="True" ></Condition>
                        <Condition Property="Control.IsMouseOver" Value="True" ></Condition>
                    </MultiTrigger.Conditions>
                    <MultiTrigger.Setters >
                        <Setter Property="Control.Foreground" Value="Blue"></Setter>
                    </MultiTrigger.Setters>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Height="100" Style="{StaticResource BigFontButton}" Margin="5" Name="button1" Content="Button"></Button>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="206,271,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>
View Code

 事件触发器

<Window x:Class="_2014_11_03_29触发器.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="BigFontButton" >
            <Style.Setters>
                <Setter Property="Control.FontFamily" Value="Times New Roman"></Setter>
                <Setter Property="Control.FontSize" Value="32"></Setter>
            </Style.Setters>
            <Style.Triggers>               
                <!--<Trigger  Property="Button.IsPressed" Value="True">
                    <Setter Property="Control.Foreground" Value="Blue"></Setter>
                </Trigger>
                <Trigger  Property="Control.IsFocused" Value="True">
                    <Setter Property="Control.Foreground" Value="DarkRed"></Setter>
                </Trigger>-->
                <!--<MultiTrigger>
                    <MultiTrigger.Conditions>--><!--表示多个条件为真的时候,才触发事件--><!--
                        <Condition Property="Control.IsFocused" Value="True" ></Condition>
                        <Condition Property="Control.IsMouseOver" Value="True" ></Condition>
                    </MultiTrigger.Conditions>
                    <MultiTrigger.Setters >
                        <Setter Property="Control.Foreground" Value="Blue"></Setter>
                    </MultiTrigger.Setters>
                </MultiTrigger>-->
                <EventTrigger RoutedEvent="Mouse.MouseEnter">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="FontSize" To="48"></DoubleAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
                <EventTrigger RoutedEvent="Mouse.MouseLeave">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.5" Storyboard.TargetProperty="FontSize" ></DoubleAnimation>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Height="100" Style="{StaticResource BigFontButton}" Margin="5" Name="button1" Content="Button"></Button>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="206,271,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
    </Grid>
</Window>
View Code

 视频三十:行为(行为,获取行为支持,创建行为,使用行为) Expression Blend 3 SDK  (http://tinyurl.com/kkp4g8)

 由于涉及到类库,未能实现全部的代码。

<Window x:Class="_2014_11_03_30行为.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Canvas Name="canvas1">
        <Rectangle Canvas.Left="10" Canvas.Top="10" Fill="Yellow" Width="80" Height="60"></Rectangle>
        <Ellipse Canvas.Left="80" Canvas.Top="70" Name="ellipse1" Fill="blue" Width="80" Height="60"></Ellipse>
    </Canvas>
</Window>
View Code

视频三十一:形状(理解形状,Shape类,矩形和椭圆,使用Viewbox控件缩放形状,直线,折线,多边形,直线线帽和直线交点,点划线)

 矩形和椭圆代码:

<Window x:Class="_2014_11_18_31形状.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>       
        <Rectangle  RadiusX="10" RadiusY="100" Height="100" Name="rectangle1"  Fill="Red" Stroke="Black"   Width="200">  </Rectangle>
        <Ellipse Grid.Row="1" Fill="Yellow" Stroke="Black" Margin="10"  Stretch="Uniform"></Ellipse>
    </Grid>
</Window>
View Code

使用Viewbox控件缩放形状代码:

<Window x:Class="_2014_11_18_31形状.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>       
        <!--<Rectangle  RadiusX="10" RadiusY="100" Height="100" Name="rectangle1"  Fill="Red" Stroke="Black"   Width="200">  </Rectangle>
        <Ellipse Grid.Row="1" Fill="Yellow" Stroke="Black" Margin="10"  Stretch="Uniform"></Ellipse>-->
        <TextBlock>The first row of a </TextBlock>
        <Viewbox Grid.Row="1" HorizontalAlignment="Left">
            <Canvas Width="200" Height="150">
                <Rectangle Fill="Yellow" Stroke="blue" Canvas.Left="30" Canvas.Top=" 40" Height="60" Width="100" HorizontalAlignment="Left"></Rectangle>
                <Ellipse Fill="Yellow" Stroke="blue" Canvas.Left="10" Canvas.Top="50" Width="100" Height="50" HorizontalAlignment="Left"></Ellipse>
               </Canvas>
        </Viewbox>
    </Grid>
</Window>
View Code

 直线,折线,多边形代码:

<Window x:Class="_2014_11_18_31形状.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>       
        <!--<Rectangle  RadiusX="10" RadiusY="100" Height="100" Name="rectangle1"  Fill="Red" Stroke="Black"   Width="200">  </Rectangle>
        <Ellipse Grid.Row="1" Fill="Yellow" Stroke="Black" Margin="10"  Stretch="Uniform"></Ellipse>-->
        <TextBlock>The first row of a </TextBlock>
        <Viewbox Grid.Row="1" HorizontalAlignment="Left">
            <Canvas Width="200" Height="150">          
                <Line Stroke="red" X1="0" Y1="0" X2="10" Y2="100" Canvas.Left="150" Canvas.Top="20"></Line><!--直线-->
                <Rectangle Fill="Yellow" Stroke="blue" Canvas.Left="30" Canvas.Top=" 40" Height="60" Width="100" HorizontalAlignment="Left"></Rectangle><!--矩形-->
                <Ellipse Fill="Yellow" Stroke="blue" Canvas.Left="10" Canvas.Top="50" Width="100" Height="50" HorizontalAlignment="Left"></Ellipse><!--椭圆-->
                <Polyline Stroke="Blue" Points="5,5 15,50, 50 ,80 100,1"></Polyline><!--折线-->
                <Polygon Stroke="Blue" Points="10,10 15,50, 50 ,80 100,1" Canvas.Left="100" ></Polygon><!--多边形-->
            </Canvas>
        </Viewbox>
    </Grid>
</Window>
View Code

 直线线帽和直线交点:

   <Line StrokeThickness="15" StrokeStartLineCap="Triangle" StrokeEndLineCap="Round" Stroke="red" X1="0" Y1="0" X2="10" Y2="100" Canvas.Left="150" Canvas.Top="20"></Line><!--直线-->

 其中,StrokeThickness="15" 设置线的粗细。

         StrokeStartLineCap="Triangle" 设置起点的样式

         StrokeEndLineCap="Round"  设置终点的样式

点划线代码:

  <Polyline StrokeDashArray="1 5" StrokeThickness="2" Stroke="Blue" Points="5,5 15,50, 50 ,80 100,1"></Polyline><!--折线-->

其中,StrokeDashArray="1 5"    1 是使用实线的位置, 5是虚线的位置。 StrokeThickness="15" 设置线的粗细。

                                          

相关推荐