WPF实例图解

1 StackPanel

 


    
        
            请输入昵称:
            
            
        
    


WPF实例图解_第1张图片

 

 

 

 

2 选项卡

 


    
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
        
            
        
    


WPF实例图解_第2张图片

 

 

 

 

3 grid

 


    
        
            
            
            
            
        
        
            
            
            
            
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    


WPF实例图解_第3张图片

 

 

 

 

4 groupbox

 


    
        
            
                
                    
                    
                
            
            
                
                    
                    
                    
                    
                    
                
            
        
    


WPF实例图解_第4张图片

 

 

 

 

5 WPF版进程管理器

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.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace EnumProcess
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        ObservableCollection processInfoList = new ObservableCollection();

        internal ObservableCollection 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;
        }
    }

}


WPF实例图解_第5张图片

 

 

 

 

6 dockpanel

 


    
        
                    
            
            
            
            
        
    


WPF实例图解_第6张图片

 

 

 

 

7 wpf 2d 图形

 


    
        

            

            

            

            

        

        

            

            

            

            

        

        
            
                
                    
                    
                    
                
            
        

    


WPF实例图解_第7张图片

 

 

 

 

8 基本3D

 


    
        
            
                
            

            
            
                
                    
                        
                    
                    
                        
                    
                
            
        
    
    


WPF实例图解_第8张图片

 

 

 

 

9 wpf 特效按钮

项目代码在此;

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

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

WPF实例图解_第9张图片

 

WPF实例图解_第10张图片

 

WPF实例图解_第11张图片

 

WPF实例图解_第12张图片

 

10 wpf依赖属性的说明

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.Navigation;
using System.Windows.Shapes;

namespace btnspace
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    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();
        }
    }
}


WPF实例图解_第13张图片

 

 

 

 

11 控件模板

 


    
        
        
            
                
            
            
                
                    
                    
                
                
                
            
        

    
    
        
            
        
        
    

 

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
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    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;

 


    
        
            
            
                
                    
                    
                
            

            
                
                
                    
                        
                        
                    
                

                
                
                
                
                

                
                
                    
                    
                        
                        
                    
                
                
                    
                        
                    
                

                
                    
                        
                    
                
            
            
            
                
                


SimpleAniWindow.xaml;

 

 


    
        
            
                
            
                
                

            
                

            

        
        
        
        


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
{
    /// 
    /// SampleAniWindow.xaml 的交互逻辑
    /// 
    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);
        }
    }
}


WPF实例图解_第14张图片

 

 

 

 

WPF实例图解_第15张图片

 

项目在此下载;

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

AnimationDemo.rar;

 

13 绘制例子

 


    
        
            

            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
        
    


WPF实例图解_第16张图片

 

 

 

 

 

 

14 路径的使用

PathAnimationWindow.xaml;

 



    
        
            
                
                    
                
            
            
                
                    
                
            
        
    
    
        
            
        
    

    
        
        
    

 

 

 

项目在此下载;

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

PathDemo.rar;

 

WPF实例图解_第17张图片

 

WPF实例图解_第18张图片

 

WPF实例图解_第19张图片

 

WPF实例图解_第20张图片

 

WPF实例图解_第21张图片

 

15 3D立方体和光照

 

 


    
        
            
                
            
            
                
                    
                        

                        
                            
                                
                            
                            
                                
                            

                            
                                
                            
                        
                    
                

            
        
        

 

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
{

    /// 
    /// Interaction logic for MainWindow.xaml
    /// 

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


WPF实例图解_第22张图片

 

 

 

 

 

 

你可能感兴趣的:(.Net)