WPF实例图解

2016-10-05 19:19:15来源:CSDN作者:bcbobo21cn人点击

1 StackPanel

<Window x:Class="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="525">    <Grid>        <StackPanel Orientation="Horizontal">            <TextBlock>请输入昵称:</TextBlock>            <TextBox Margin="20"></TextBox>            <Button Click="Button_Click">确定</Button>        </StackPanel>    </Grid></Window>



2 选项卡

<Window x:Class="Tab1.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">    <TabControl>        <TabItem Header="2D示例的房子">            <Frame Source="HouseUsing2DDrawing.xaml"/>        </TabItem>        <TabItem Header="3D示例的房子">            <Frame Source="HouseUsing3D.xaml"/>        </TabItem >        <TabItem Header="用MatrixCamera模拟透视相机">            <Frame Source="PerspectiveByMatrixCamera.xaml"/>        </TabItem >        <TabItem Header="使用ScreenSpaceLines3D绘制的3D坐标系">            <Frame Source="Coordinates.xaml"/>        </TabItem >        <TabItem Header="立方体">            <Frame Source="Cube.xaml"/>        </TabItem >        <TabItem Header="球">            <Frame Source="Sphere.xaml"/>        </TabItem >        <TabItem Header="不同光源">            <Frame Source="Light.xaml"/>        </TabItem>        <TabItem Header="不同材质">            <Frame Source="Material.xaml"/>        </TabItem>    </TabControl></Window>



3 grid

<Window x:Class="Grid.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/>        </Grid.RowDefinitions>        <Grid.ColumnDefinitions>            <!--定义列集合-->            <ColumnDefinition/>            <ColumnDefinition/>            <ColumnDefinition/>        </Grid.ColumnDefinitions>        <Button Grid.Row="0" Grid.Column="0"></Button>        <!--添加按钮-->        <Button Grid.Row="0" Grid.Column="1"></Button>        <!--添加按钮-->        <Button Grid.Row="0" Grid.Column="2"></Button>        <!--添加按钮-->        <Button Grid.Row="1" Grid.Column="0"></Button>        <!--添加按钮-->        <Button Grid.Row="1" Grid.Column="1"></Button>        <!--添加按钮-->        <Button Grid.Row="1" Grid.Column="2"></Button>        <!--添加按钮-->        <Button Grid.Row="2" Grid.Column="0"></Button>        <!--添加按钮-->        <Button Grid.Row="2" Grid.Column="1"></Button>        <!--添加按钮-->        <Button Grid.Row="2" Grid.Column="2"></Button>        <!--添加按钮-->    </Grid></Window>



4 groupbox

<Window x:Class="GroupBox1.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>            <GroupBox.Header>                <WrapPanel>                    <Image Source ="G:/C#Prj/WPF/GroupBox1/GroupBox1/bin/Debug/green1.jpg"/>                    <TextBlock Text="我的宠物列表:"/>                </WrapPanel>            </GroupBox.Header>            <GroupBox.Content>                <StackPanel  TextBlock.FontSize="20" >                    <TextBlock Text="小兔子"/>                    <TextBlock Text="小猫咪"/>                    <TextBlock Text="小狗狗"/>                    <TextBlock Text="XXXXXXXXXXX"/>                    <TextBlock Text="YYYYYYYYYYY"/>                </StackPanel>            </GroupBox.Content>        </GroupBox>    </Grid></Window>



5 WPF版进程管理器

xaml;

<Window x:Class="EnumProcess.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="WPF版进程管理器" Height="646" Width="634" WindowStartupLocation="CenterScreen">       <Grid>        <Grid.RowDefinitions>            <RowDefinition />            <RowDefinition />            <RowDefinition />            <RowDefinition />                  <RowDefinition />            <RowDefinition />            <RowDefinition />              </Grid.RowDefinitions>        <Grid.ColumnDefinitions>            <ColumnDefinition />            <ColumnDefinition />            <ColumnDefinition />                  </Grid.ColumnDefinitions>        <Button Grid.Row="6" Grid.Column="0" Content="刷新" Name="button1" Margin="5" Background="{DynamicResource myBrush}"/>        <Button Grid.Row="6" Grid.Column="1" Content="新建进程" Name="button2" Margin="5" >            <Button.Background>                <LinearGradientBrush EndPoint="0.123,0.595" StartPoint="0.123,-0.243" SpreadMethod="Reflect">                    <GradientStop Color="#FFFDFDFD" Offset="0"/>                    <GradientStop Color="#FFF5AD10" Offset="1"/>                </LinearGradientBrush>            </Button.Background>        </Button>        <Button Grid.Row="6" Grid.Column="3" Name="animationBtn" Margin="5">            <Button.Background>                <SolidColorBrush x:Name="AnimBrush"/>            </Button.Background>            <Button.Triggers>                <EventTrigger RoutedEvent="Button.Loaded">                    <BeginStoryboard>                        <Storyboard>                            <ColorAnimation            Storyboard.TargetName="AnimBrush"             Storyboard.TargetProperty="(SolidColorBrush.Color)"            From="Red" To="Green" Duration="0:0:5"             AutoReverse="True" RepeatBehavior="Forever" />                        </Storyboard>                    </BeginStoryboard>                </EventTrigger>            </Button.Triggers>            结束进程        </Button>        <ListView Grid.ColumnSpan="3" Grid.RowSpan="6" Margin="10" Name="listView1" Background="#FF7FD87F" FontSize="16">            <ListView.View>                <GridView>                    <GridViewColumn Header="进程名称" Width="200" DisplayMemberBinding="{Binding Path=pname}"/>                    <GridViewColumn Header="进程ID" Width="200" DisplayMemberBinding="{Binding Path=pid}"/>                    <GridViewColumn Header="内存使用" Width="200" DisplayMemberBinding="{Binding Path=pworkset}"/>                   </GridView>            </ListView.View>        </ListView>            </Grid></Window>

cs;

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Diagnostics;using System.Collections.Generic;using System.Collections.ObjectModel;namespace EnumProcess{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        ObservableCollection<processInfo> processInfoList = new ObservableCollection<processInfo>();        internal ObservableCollection<processInfo> ProcessInfoList        {            get { return processInfoList; }            set { processInfoList = value; }        }               public MainWindow()        {            InitializeComponent();            RefreshList();            listView1.ItemsSource = processInfoList;                }                private void RefreshList()        {            Process[] processes;            processes = Process.GetProcesses();            foreach (Process instance in processes)            {                string str1,str2,str3;                str1=instance.ProcessName;                str2=instance.Id.ToString();                str3 = instance.WorkingSet64.ToString();                processInfoList.Add(new processInfo(str1,str2,str3));            }        }    }    class processInfo    {        private string _pname;        private string _pid;        private string _pworkset;                public string pname        {            get { return _pname; }            set { _pname = value; }        }        public string pid        {            get { return _pid; }            set { _pid = value; }        }        public string pworkset        {            get { return _pworkset; }            set { _pworkset = value; }        }        public processInfo(string name, string id, string workset)        {            _pname = name;            _pid = id;            _pworkset = workset;        }    }}



6 dockpanel

<Window x:Class="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">    <Grid>        <DockPanel>            <Button DockPanel.Dock="Top" Height="50">button1</Button>                    <Button DockPanel.Dock="Left,Bottom">button2</Button>            <Button DockPanel.Dock="Right">button3</Button>            <Button DockPanel.Dock="Bottom">button4</Button>            <Button DockPanel.Dock="Bottom" >button5</Button>        </DockPanel>    </Grid></Window>



7 wpf 2d 图形

<Window x:Class="TD1.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="WPF 2D 图形示例" Height="350" Width="525">    <Grid Background="#4F007DC8">        <Canvas Height="200" Width="250"  HorizontalAlignment="Left">            <Ellipse          Width="100"          Height="50"          Fill="#CCCCFF"          Canvas.Left="10"          Canvas.Top="25" />            <Ellipse          Width="100"          Height="50"          Fill="#CCCCFF"          Stroke="Black"          StrokeThickness="4"          Canvas.Left="10"          Canvas.Top="100"/>            <Ellipse          Width="50"          Height="50"          Fill="#CCCCFF"          Canvas.Left="135"          Canvas.Top="25"/>            <Ellipse        Width="50"        Height="50"          Stroke="Black"          StrokeThickness="4"          Canvas.Left="135"        Canvas.Top="100" />        </Canvas>        <Canvas Height="200" Width="250"  HorizontalAlignment="Left">            <Rectangle        Width="100"        Height="50"        Fill="#CCCCFF"        Canvas.Left="10"        Canvas.Top="25" />            <Rectangle        Width="100"        Height="50"        Fill="#CCCCFF"        Stroke="Black"        StrokeThickness="4"        Canvas.Left="10"        Canvas.Top="100"/>            <Rectangle        Width="100"        Height="50"        RadiusX="20"        RadiusY="20"        Fill="#CCCCFF"        Canvas.Left="135"        Canvas.Top="25"/>            <Rectangle      Width="100"      Height="50"      RadiusX="20"      RadiusY="20"        Stroke="Black"        StrokeThickness="4"        Canvas.Left="135"      Canvas.Top="100" />        </Canvas>        <Path Stroke="Black" StrokeThickness="1" Fill="#CCCCFF">            <Path.Data>                <GeometryGroup FillRule="Nonzero">                    <LineGeometry StartPoint="10,10" EndPoint="50,30" />                    <EllipseGeometry Center="40,70" RadiusX="30" RadiusY="30" />                    <RectangleGeometry Rect="30,55 100 30" />                </GeometryGroup>            </Path.Data>        </Path>    </Grid></Window>



8 基本3D

<Window x:Class="ThD1.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>        <Viewport3D>            <Viewport3D.Camera>                <PerspectiveCamera Position="-2,2,2" LookDirection="2,-2,-2" UpDirection="0,1,0"/>            </Viewport3D.Camera>            <ModelVisual3D>            <ModelVisual3D.Content>                <GeometryModel3D>                    <GeometryModel3D.Geometry>                        <MeshGeometry3D Positions="1,0,0 0,-1,0 0,0,1"/>                    </GeometryModel3D.Geometry>                    <GeometryModel3D.Material>                        <DiffuseMaterial Brush="Yellow" />                    </GeometryModel3D.Material>                </GeometryModel3D>            </ModelVisual3D.Content>        </ModelVisual3D>    </Viewport3D>    </Grid></Window>



9 wpf 特效按钮

项目代码在此;

http://pan.baidu.com/s/1o7OEMc6

此代码来自 wpf编程-葵花宝典 一书;









10 wpf依赖属性的说明

xaml;

<Window x:Class="btnspace.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        xmlns:local ="clr-namespace:btnspace"        Title="btnspace" Width="300" Height="300">    <Grid>        <Grid.RowDefinitions>            <RowDefinition/>            <RowDefinition/>        </Grid.RowDefinitions>        <local:SpaceButton x:Name="btnSpace" Grid.Column="0" Grid.Row="0" Margin="5" Click="btnSpace_Click" Text="设置按钮字符空格:3">        </local:SpaceButton>        <local:SpaceButton x:Name= "winSpace" Grid.Column="0" Grid.Row="1" Margin="5" Click="winSpace_Click" Text="设置窗口字符空格:3">        </local:SpaceButton>    </Grid></Window>

cs;

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace btnspace{    /// <summary>    /// Interaction logic for MainWindow.xaml    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void btnSpace_Click(object sender, RoutedEventArgs e)        {            this.btnSpace.Space = 3;        }        private void winSpace_Click(object sender, RoutedEventArgs e)        {            this.Space = 3;        }        static MainWindow()        {            FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();            metadata.Inherits = true;            SpaceProperty = SpaceButton.SpaceProperty.AddOwner(typeof(MainWindow));            SpaceProperty.OverrideMetadata(typeof(Window),metadata);        }        public static readonly DependencyProperty SpaceProperty;        public int Space        {            set            {                SetValue(SpaceProperty, value);            }            get            {                return (int)GetValue(SpaceProperty);            }        }    }}

SpaceButton.cs

using System;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Input;using System.Windows.Media;namespace btnspace{    public class SpaceButton : Button    {        // 传统.Net做法 私有字段搭配一个公开属性        string txt;        public string Text        {            set            {                txt = value;                Content = SpaceOutText(txt);            }            get            {                return txt;            }        }        // 依赖属性        public static readonly DependencyProperty SpaceProperty;        //.Net属性包装器        public int Space        {            set            {                SetValue(SpaceProperty, value);            }            get            {                return (int)GetValue(SpaceProperty);            }        }        // 静态的构造函数        static SpaceButton()        {            // 定义元数据            FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();            metadata.DefaultValue = 0;            metadata.Inherits = true;            metadata.PropertyChangedCallback += OnSpacePropertyChanged;            // 注册依赖属性            SpaceProperty = DependencyProperty.Register("Space", typeof(int), typeof(SpaceButton), metadata, ValidateSpaceValue);        }        // 值验证的回调函数        static bool ValidateSpaceValue(object obj)        {            int i = (int)obj;            return i >= 0;        }        // 属性值改变的回调函数        static void OnSpacePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)        {            SpaceButton btn = obj as SpaceButton;            string txt = btn.Content as string;            if (txt == null) return;            btn.Content = btn.SpaceOutText(txt);        }        // 该方法给字符串间距加上空格        string SpaceOutText(string str)        {            if (str == null)                return null;            StringBuilder build = new StringBuilder();            // 往里面加上Space个空格            foreach (char ch in str)                build.Append(ch + new string(' ', Space));            return build.ToString();        }    }}



11 控件模板

<Window x:Class="button03.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="MainWindow" SizeToContent="WidthAndHeight" Loaded="Window_Loaded">    <Window.Resources>        <Style TargetType="{x:Type Button}">            <Setter Property="Background" Value="Red"/>        </Style>        <ControlTemplate TargetType="{x:Type Button}" x:Key="ButtonTemplate">            <ControlTemplate.Resources>                <Style TargetType="{x:Type Button}">                    <Setter Property="Background" Value="Yellow"/>                </Style>            </ControlTemplate.Resources>            <Grid Background="Green">                <Grid.ColumnDefinitions>                    <ColumnDefinition/>                    <ColumnDefinition/>                </Grid.ColumnDefinitions>                <Button  x:Name="btn1" Background ="Blue"  Grid.Column="0"   Width="180" Height="50" >                </Button>                <Ellipse Grid.Column="1" Fill="{Binding RelativeSource={RelativeSource TemplatedParent}, Path = Background}" Width="20" Height="20" />            </Grid>        </ControlTemplate>    </Window.Resources>    <Grid Name="Re">        <Grid.RowDefinitions>            <RowDefinition />        </Grid.RowDefinitions>        <Button x:Name="btn" Margin ="5"  Grid.Row="0" Template="{StaticResource ButtonTemplate}"></Button>    </Grid></Window>

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 button03{    /// <summary>    /// Interaction logic for MainWindow.xaml    /// </summary>    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private void Window_Loaded(object sender, RoutedEventArgs e)        {            Button obj = btn.Template.FindName("btn1", btn) as Button;                    }    }}



12 动画演示

XzWindow.xaml;

<Window x:Class="AnimationDemo.XzWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="XzWindow" Height="800" Width="800" WindowStartupLocation="CenterScreen">    <Grid>        <StackPanel>            <!-- StackPanel面版的背景色设置 -->            <StackPanel.Background>                <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">                    <GradientStop Color="#FFFFB5D6" Offset="0"/>                    <GradientStop Color="#F0087272" Offset="1"/>                </LinearGradientBrush>            </StackPanel.Background>            <Canvas Width="400" Height="200" >                <!-- Canvas面版的背景色设置 -->                <Canvas.Background>                    <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">                        <GradientStop Color="#FFA6B5D6" Offset="0"/>                        <GradientStop Color="#FFE87272" Offset="1"/>                    </LinearGradientBrush>                </Canvas.Background>                <!-- 旋转时当背景的三个Rectangle位置坐标(分别从左到右) -->                <Rectangle Canvas.Top="80" Canvas.Left="50" Fill="Blue" Width="50" Height="50" Stroke="Black"          StrokeThickness="5" Opacity="0.25" />                <!-- Rectangle中代表的属性值含义:  Canvas.Top:Rectangle左上角到Canvas顶部的距离;Canvas.Left:Rectangle左上角到Canvas左边的距离;Fill:设置Rectangle内的填充色;Width:Rectangle的宽度;Height:Rectangle的高度;Stroke:Rectangle的边框颜色;StrokeThickness:Rectangle的边框粗细;Opacity:Rectangle的透明度 -->                <Rectangle Canvas.Top="80" Canvas.Left="180" Fill="Blue" Width="50" Height="50" Stroke="Black"          StrokeThickness="5" Opacity="0.25" />                <Rectangle Canvas.Top="80" Canvas.Left="300" Fill="Blue" Width="50" Height="50" Stroke="Black"          StrokeThickness="5" Opacity="0.25" />                <!-- 表面旋转的三个Rectangle -->                <Rectangle           Canvas.Top="80" Canvas.Left="50" Fill="Blue"          Width="50" Height="50" Stroke="Black"          StrokeThickness="5">                    <!-- 使Rectangle变形 -->                    <Rectangle.RenderTransform>                        <!-- RotateTransform使Rectangle旋转,并给这个要旋转的Rectangle起个名字,设置它的初始角度,及中心坐标 -->                        <RotateTransform x:Name="MyCenteredRotation"              Angle="0" CenterX="25" CenterY="25" />                    </Rectangle.RenderTransform>                </Rectangle>                <Rectangle          Canvas.Top="80" Canvas.Left="180" Fill="Blue"          Width="50" Height="50" Stroke="Black"          StrokeThickness="5">                    <Rectangle.RenderTransform>                        <RotateTransform x:Name="MyRotationAboutOrigin"              Angle="0" CenterX="0" CenterY="0" />                    </Rectangle.RenderTransform>                </Rectangle>                <Rectangle          Canvas.Top="80" Canvas.Left="300" Fill="Blue"          Width="50" Height="50" Stroke="Black"          StrokeThickness="5">                    <Rectangle.RenderTransform>                        <RotateTransform x:Name="MyAnimatedCenterRotation"            Angle="0" CenterX="0" CenterY="0" />                    </Rectangle.RenderTransform>                </Rectangle>            </Canvas>            <!-- 控制部分 -->            <StackPanel Orientation="Horizontal">                <!-- 四个控制按键 -->                <Button Name="startButton" Content="Start" />                <Button Name="pauseButton" Content="Pause" />                <Button Name="resumeButton" Content="Resume" />                <Button Name="stopButton" Content="Stop" />                <!-- 触发器及故事板的组合 -->                <StackPanel.Triggers>                    <!-- 很好翻译了:在按键startButton被点击的时候触发 -->                    <EventTrigger SourceName="startButton" RoutedEvent="Button.Click">                        <!-- 事件触发以后做什么呢?由故事板来解决,它来产生动画效果。 -->                        <BeginStoryboard Name="myBeginStoryboard">                            <!-- 瞧瞧!这个故事板里有几个”故事“, 有五个! -->                            <Storyboard Name="myStoryboard">                                <!-- 对!这就是一个故事。那么这个故事写的是什么呢? -->                                <!-- 首先,故事的主角要找到,它是上面Rectangle的一个实例MyCenteredRotation。这个故事表现主角的那一个方面呢,那就是Angle.当然故事发生的时候在4秒内MyCenteredRotation的Angle由0度增长到360度,换言之就是旋转两圈。并且永远这样旋转下去(RepeatBehavior="Forever"),下面的故事雷同。-->                                <DoubleAnimation Storyboard.TargetName="MyCenteredRotation"                       Storyboard.TargetProperty="Angle"                            From="0" To="360" RepeatBehavior="Forever" Duration="0:0:4"/>                                <DoubleAnimation Storyboard.TargetName="MyRotationAboutOrigin"                       Storyboard.TargetProperty="Angle"                            From="0" To="360" RepeatBehavior="Forever" Duration="0:0:4"/>                                <!-- 大牌明星MyAnimatedCenterRotation有三场戏要拍 -->                                <DoubleAnimation                        Storyboard.TargetName="MyAnimatedCenterRotation"                       Storyboard.TargetProperty="Angle"                            From="0" To="360" RepeatBehavior="Forever" Duration="0:0:4"/>                                <!-- 下面两场可是在同时进行哟! -->                                <DoubleAnimation Storyboard.TargetName="MyAnimatedCenterRotation"                       Storyboard.TargetProperty="CenterX"                            From="0" To="50" RepeatBehavior="Forever" Duration="0:0:16"/>                                <DoubleAnimation Storyboard.TargetName="MyAnimatedCenterRotation"                       Storyboard.TargetProperty="CenterY"                            From="0" To="50" RepeatBehavior="Forever" Duration="0:0:16"/>                            </Storyboard>                        </BeginStoryboard>                    </EventTrigger>                    <!-- 同理可证:pauseButton被点击的时候故事全部暂停 :PauseStoryboard -->                    <EventTrigger SourceName="pauseButton" RoutedEvent="Button.Click" >                        <PauseStoryboard BeginStoryboardName="myBeginStoryboard">                        </PauseStoryboard>                    </EventTrigger>                    <!-- 同理可证:pauseButton被点击的时候故事全部重新开始 :ResumeStoryboard -->                    <EventTrigger RoutedEvent="Button.Click" SourceName="resumeButton">                        <ResumeStoryboard BeginStoryboardName="myBeginStoryboard">                        </ResumeStoryboard>                    </EventTrigger>                    <!-- 同理可证:pauseButton被点击的时候故事全部重新完全停掉 :StopStoryboard -->                    <EventTrigger RoutedEvent="Button.Click" SourceName="stopButton">                        <StopStoryboard BeginStoryboardName="myBeginStoryboard">                        </StopStoryboard>                    </EventTrigger>                </StackPanel.Triggers>            </StackPanel>        </StackPanel>    </Grid></Window>

SimpleAniWindow.xaml;

<Window x:Class="AnimationDemo.SampleAniWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="SampleAniWindow" Height="700" Width="1000" WindowStartupLocation="CenterScreen">    <Grid Loaded="Grid_Loaded">        <Grid.Triggers>            <EventTrigger RoutedEvent="Grid.Loaded">                <BeginStoryboard>            <Storyboard Name="RoundStory1" AutoReverse="True" RepeatBehavior = "1" >                <DoubleAnimation From="0" To="200" Duration="0:0:2" Storyboard.TargetName="ellipse2" Storyboard.TargetProperty="Width"></DoubleAnimation>                <DoubleAnimation From="0" To="200" Duration="0:0:2" Storyboard.TargetName="ellipse2" Storyboard.TargetProperty="Height"></DoubleAnimation>            </Storyboard>                </BeginStoryboard>            </EventTrigger>        </Grid.Triggers>        <Ellipse Height="200" HorizontalAlignment="Left" Fill="Green" Margin="0,0,0,0" Name="ellipse1" Stroke="Black" VerticalAlignment="Top" Width="200" />        <Ellipse Height="0" Name="ellipse2" HorizontalAlignment="Left" Stroke="Black" Fill="White" VerticalAlignment="Top" Width="0" />        <Button Content="Button" Height="67" HorizontalAlignment="Left" Margin="473,553,0,0" Name="button1" VerticalAlignment="Top" Width="150" Click="button1_Click" />        <Button Content="DoubleAnimation" Height="70" HorizontalAlignment="Left" Margin="379,28,0,0" Name="button2" VerticalAlignment="Top" Width="176" Click="button2_Click" />    </Grid></Window>

SimpleAniWindow.xaml.cs;

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Shapes;using System.Windows.Media.Animation;namespace AnimationDemo{    /// <summary>    /// SampleAniWindow.xaml 的交互逻辑    /// </summary>    public partial class SampleAniWindow : Window    {        public SampleAniWindow()        {            InitializeComponent();                    }        private void Grid_Loaded(object sender, RoutedEventArgs e)        {                    }        private void button1_Click(object sender, RoutedEventArgs e)        {            DoubleAnimation widthAnimation =                new DoubleAnimation(200, 400, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.HoldEnd);            //指定高度变化的起点,终点与持续时间,并在动画结束时保持大小            DoubleAnimation heightAnimation =               new DoubleAnimation(50, 100, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.HoldEnd);            //开始动画            //变化不是阻塞的,而是异步,所以看上去长度与高度几乎是同时变化            button1.BeginAnimation(Button.WidthProperty, widthAnimation);            button1.BeginAnimation(Button.HeightProperty, heightAnimation);        }        private void button2_Click(object sender, RoutedEventArgs e)        {            DoubleAnimation widthAnimation =                new DoubleAnimation(200, 400, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.Stop);            //指定高度变化的起点,终点与持续时间,并在动画结束时恢复原值            DoubleAnimation heightAnimation =                new DoubleAnimation(50, 100, new Duration(TimeSpan.FromSeconds(0.8)), FillBehavior.Stop);            //开始动画            //变化不是阻塞的,而是异步,所以看上去长度与高度几乎是同时变化            button2.BeginAnimation(Button.WidthProperty, widthAnimation);            button2.BeginAnimation(Button.HeightProperty, heightAnimation);        }    }}





项目在此下载;

http://pan.baidu.com/s/1o7OEMc6

AnimationDemo.rar;


13 绘制例子

<Window x:Class="DrawDemo.stripeWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="stripeWindow" Height="768" Width="1024" WindowStartupLocation="CenterScreen">    <Grid>        <Canvas Width="1000" Name="canvas1" Margin="189,0,58,23">            <Polygon Points="10 300,10 10,60 10,60 250,150 250,150 10,200 10,200 300 " Stroke="Green" Fill="Green" Margin="48,51,754,378" />            <Ellipse Height="150" Width="150" Fill="Green" Canvas.Left="345" Canvas.Top="51" />            <Ellipse Height="100" Width="100" Fill="White" Margin="360,113,542,516" Canvas.Left="10" Canvas.Top="-36" />                        <Polygon Points="450 40,450 10,680 10,520 200,680 200,680 230,450 230,620 40" Fill="Green" Stroke="Green" Margin="199,51,122,447" />                        <Polygon Points="100 500,130 510,40 610,10 600 " Fill="Green" Stroke="Green" Height="678" Canvas.Left="233" Canvas.Top="-57" />            <Ellipse Width="39" Fill="Green" Margin="194,593,0,97" Height="38" Canvas.Left="39" Canvas.Top="-55" />            <Rectangle Canvas.Left="362" Canvas.Top="427" Height="158" Name="rectangle1" Fill="Green" Stroke="Black" Width="150" />            <Rectangle Canvas.Left="399" Canvas.Top="462" Height="97" Name="rectangle2" Fill="White" Stroke="Black" Width="79" />            <Rectangle Canvas.Left="412" Canvas.Top="367" Height="62" Name="rectangle3" Fill="Green" Stroke="Black" Width="35" />            <Ellipse Canvas.Left="345" Canvas.Top="228" Height="142" Name="ellipse1" Fill="Green" Stroke="Black" Width="153" />            <Ellipse Canvas.Left="370" Canvas.Top="265" Height="26" Name="ellipse2" Stroke="Black" Width="25" />            <Ellipse Canvas.Left="443" Canvas.Top="265" Height="27" Name="ellipse3" Stroke="Black" Width="27" />            <Ellipse Canvas.Left="399" Canvas.Top="330" Height="21" Name="ellipse4" Stroke="Black" Width="55" />            <Polygon Canvas.Left="264" Canvas.Top="77" Fill="Green" Height="678" Points="100 500,130 510,40 610,10 600 " Stroke="Green" />        </Canvas>            </Grid></Window>





14 路径的使用

PathAnimationWindow.xaml;

<Window    x:Class="PathDemo.PathAnimationWindow"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="路径动画"    Width="640" Height="480" WindowStartupLocation="CenterScreen">    <Window.Resources>        <Storyboard x:Key="Timeline1" RepeatBehavior = "Forever" AutoReverse="True">            <DoubleAnimationUsingPath BeginTime="00:00:00" Duration="00:00:02" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" Source="X">                <DoubleAnimationUsingPath.PathGeometry>                    <PathGeometry Figures="M-68,149.00022 C-39.695816,109.3744 -17.383496,83.397528 39,74.000287 C73.433618,68.261356 91.198465,71.210009 125,89.000273 C157.86626,106.29829 181.01028,123.79991 204,155.00021 C213.7418,168.22121 224.03351,180.96708 234,194.00017 C246.07551,209.79121 257.36571,220.41957 275,231.00014 C285.57406,237.34456 295.83653,242.84775 307,248.00012 C317.8183,252.99318 324.46505,252.89547 337,252.00012 C362.92461,250.14836 384.46844,240.41978 404,225.00014"/>                </DoubleAnimationUsingPath.PathGeometry>            </DoubleAnimationUsingPath>            <DoubleAnimationUsingPath BeginTime="00:00:00" Duration="00:00:02" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Source="Y">                <DoubleAnimationUsingPath.PathGeometry>                    <PathGeometry Figures="M-68,149.00022 C-39.695816,109.3744 -17.383496,83.397528 39,74.000287 C73.433618,68.261356 91.198465,71.210009 125,89.000273 C157.86626,106.29829 181.01028,123.79991 204,155.00021 C213.7418,168.22121 224.03351,180.96708 234,194.00017 C246.07551,209.79121 257.36571,220.41957 275,231.00014 C285.57406,237.34456 295.83653,242.84775 307,248.00012 C317.8183,252.99318 324.46505,252.89547 337,252.00012 C362.92461,250.14836 384.46844,240.41978 404,225.00014"/>                </DoubleAnimationUsingPath.PathGeometry>            </DoubleAnimationUsingPath>        </Storyboard>    </Window.Resources>    <Window.Triggers>        <EventTrigger RoutedEvent="FrameworkElement.Loaded">            <BeginStoryboard Storyboard="{StaticResource Timeline1}"/>        </EventTrigger>    </Window.Triggers>    <Grid x:Name="LayoutRoot">        <Path Fill="#FFFFFFFF" Stretch="Fill" Stroke="#FF000000" Margin="63.5,128.847,87.5,133.025" Data="M64,207 C92.304184,167.37414 114.6165,141.39725 171,132 205.43362,126.26106 223.19847,129.20972 257,147 289.86626,164.29803 313.01028,181.79967 336,213 345.7418,226.22101 356.03351,238.96689 366,252 378.07551,267.79105 389.36571,278.41942 407,289 417.57406,295.34443 427.83653,300.84763 439,306 449.8183,310.99306 456.46505,310.89535 469,310 494.92461,308.14824 516.46844,298.41966536,283"/>        <Button RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Left" Margin="82,30,0,0" x:Name="button" VerticalAlignment="Top" Width="100" Height="56" Content="Button">            <Button.RenderTransform>                <TransformGroup>                    <ScaleTransform ScaleX="1" ScaleY="1"/>                    <SkewTransform AngleX="0" AngleY="0"/>                    <RotateTransform Angle="0"/>                    <TranslateTransform X="0" Y="0"/>                </TransformGroup>            </Button.RenderTransform>        </Button>    </Grid></Window>

项目在此下载;

http://pan.baidu.com/s/1o7OEMc6

PathDemo.rar;












15 3D立方体和光照

<Window x:Class="wpf3d2.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 x:Name="grid1" ShowGridLines="True">        <Viewport3D>            <Viewport3D.Camera >                <PerspectiveCamera Position="-4,1,10" LookDirection="4,-1,-10" UpDirection="0,1,0" FieldOfView="45"/>            </Viewport3D.Camera>            <ModelVisual3D>                <ModelVisual3D.Content>                    <Model3DGroup>                        <DirectionalLight Direction="0,0,-1"/>                        <GeometryModel3D>                            <GeometryModel3D.Geometry>                                <MeshGeometry3D Positions="0,1,0 1,-1,1 -1,-1,-1"                                                Normals="0,1,0 -1,0,1 1,0,1 -1,0,-1 1,0,-1"                                                TriangleIndices="0,2,1 0,3,1 0,3,4 0,2,4" />                            </GeometryModel3D.Geometry>                            <GeometryModel3D.Material>                                <DiffuseMaterial Brush="Red"/>                            </GeometryModel3D.Material>                            <GeometryModel3D.BackMaterial>                                <DiffuseMaterial Brush="Green"/>                            </GeometryModel3D.BackMaterial>                        </GeometryModel3D>                    </Model3DGroup>                </ModelVisual3D.Content>            </ModelVisual3D>        </Viewport3D>        <Button Content="Button" Height="58" HorizontalAlignment="Left" Margin="397,240,0,0" Name="button1" VerticalAlignment="Top" Width="91" Click="button1_Click" />    </Grid></Window>

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Windows.Media.Media3D;namespace wpf3d2{    /// <summary>    /// Interaction logic for MainWindow.xaml    /// </summary>    public partial class MainWindow : Window    {        Viewport3D vp = new Viewport3D();        public MainWindow()        {            InitializeComponent();            //this.Content = vp;            //            //grid1.Children.Add(vp);            SetupCamera();            GetModelVisual3D();        }        private void SetupCamera()        {            PerspectiveCamera camera = new PerspectiveCamera();            camera.Position = new Point3D(-40, 40, 40);            camera.UpDirection = new Vector3D(0, 0, 1);            camera.LookDirection = new Vector3D(40, -40, -40);            vp.Camera = camera;        }        private void GetModelVisual3D()        {            Model3DGroup mg = new Model3DGroup();            DirectionalLight dl = new DirectionalLight();            dl.Color = Colors.White; dl.Direction = new Vector3D(-1, -1, -3);            GeometryModel3D gm = new GeometryModel3D();            MeshGeometry3D meshg = new MeshGeometry3D();            Point3DCollection pc = new Point3DCollection();            pc.Add(new Point3D(0, 0, 0)); pc.Add(new Point3D(10, 0, 0)); pc.Add(new Point3D(10, 10, 0));            pc.Add(new Point3D(0, 10, 0)); pc.Add(new Point3D(0, 0, 10)); pc.Add(new Point3D(10, 0, 10));            pc.Add(new Point3D(10, 10, 10)); pc.Add(new Point3D(0, 10, 10));            meshg.Positions = pc;            Int32Collection ic = new Int32Collection();            ic.Add(0); ic.Add(1); ic.Add(3); ic.Add(1); ic.Add(2); ic.Add(3); ic.Add(0); ic.Add(4); ic.Add(3);            ic.Add(4); ic.Add(7); ic.Add(3); ic.Add(4); ic.Add(6); ic.Add(7); ic.Add(4); ic.Add(5); ic.Add(6);            ic.Add(0); ic.Add(4); ic.Add(1); ic.Add(1); ic.Add(4); ic.Add(5); ic.Add(1); ic.Add(2); ic.Add(6);            ic.Add(6); ic.Add(5); ic.Add(1); ic.Add(2); ic.Add(3); ic.Add(7); ic.Add(7); ic.Add(6); ic.Add(2);            meshg.TriangleIndices = ic;            DiffuseMaterial dm = new DiffuseMaterial();            dm.Brush = Brushes.Red;            gm.Geometry = meshg;            gm.Material = dm;            mg.Children.Add(dl);            mg.Children.Add(gm);            ModelVisual3D mv = new ModelVisual3D();            mv.Content = mg;            vp.Children.Add(mv);        }        private void Window_Loaded(object sender, RoutedEventArgs e)        {            grid1.Children.Add(vp);                    }    }}





最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台