例:button1.Margin = new Thickness(0); 可正常定位对象坐标。但如果button1用 ThicknessAnimation 动画之后,上面代码就没反应

解决方案 »

  1.   


    这里有个简单的例子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 CodeCreateDrawing
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            DrawingVisual ghostVisual;
            public Window1()
            {
                InitializeComponent();            Width = 300;
                Height = 350;
                ghostVisual = new DrawingVisual();
                using (DrawingContext dc = ghostVisual.RenderOpen())
                {                dc.DrawEllipse(Brushes.Black, new Pen(Brushes.White, 10),
                    new Point(95, 95), 15, 15);                dc.DrawEllipse(Brushes.Black, new Pen(Brushes.White, 10),
                    new Point(170, 105), 15, 15);                Pen p = new Pen(Brushes.Black, 10);
                    p.StartLineCap = PenLineCap.Round;
                    p.EndLineCap = PenLineCap.Round;
                    dc.DrawLine(p, new Point(75, 160), new Point(175, 150));
                }
                AddVisualChild(ghostVisual);
                AddLogicalChild(ghostVisual);
            }
            protected override int VisualChildrenCount
            {
                get { return 1; }
            }
            protected override Visual GetVisualChild(int index)
            {
                if (index != 0)
                    throw new ArgumentOutOfRangeException("index");
                return ghostVisual;
            }
        }
    }
    这个例子里面生成一个DrawingVisual,并把它加到Window中。更多的关于 Drawing Objects Overview我们同样可以用代码生成 ShapeShape是另外一种画图的方法。Shape是UIElement,支持高级特性,但消耗更多的资源。 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 CodeGenerateShape
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }        private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                System.Windows.Shapes.Rectangle rec = new Rectangle();            // the width and height can from data source
                rec.Width = 100;
                rec.Height = 50;
                rec.Stroke = Brushes.SkyBlue;            Canvas.SetTop(rec, 20);            Canvas.SetLeft(rec, 10);            canvas2DContainer.Children.Add(rec);
                System.Windows.Shapes.Path myPath = new Path();
                myPath.Stroke = System.Windows.Media.Brushes.Black;
                myPath.Fill = System.Windows.Media.Brushes.MediumSlateBlue;
                myPath.StrokeThickness = 4;
                myPath.HorizontalAlignment = HorizontalAlignment.Left;
                myPath.VerticalAlignment = VerticalAlignment.Center;
                EllipseGeometry myEllipseGeometry = new EllipseGeometry();
                myEllipseGeometry.Center = new System.Windows.Point(50, 50);
                myEllipseGeometry.RadiusX = 25;
                myEllipseGeometry.RadiusY = 25;
                myPath.Data = myEllipseGeometry;            canvas2DContainer.Children.Add(myPath);
            }
        }
    }
    <Window x:Class="CodeGenerateShape.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" Loaded="Window_Loaded">
        <Grid>
            <Canvas Name="canvas2DContainer">
                
            </Canvas>
        </Grid>
    </Window>
    跟多的Shape:          Shapes and Basic Drawing in WPF Overview
    Shapes How-to Topics
       用wpf动态绘图,需要些什么启示?gdi绘图是通过重载onpaint,wpf不需要如此,那我怎么确保刷新的时候,能重绘或不重绘我的图形?3D情况下,如何动态绘制多重折线或动态生成几何图形。    这个简单的说,就是,你的Visual对象是可见的时候,WPF会负责不停的重绘,比如上面的例子里面的  Shape    3D模型的情况下,根据你设定的模型,光源,照相机等,不停的显示给你照相机拍到的画面。当然你不改变这些东西的话,你看到的是静止的画面  你可以添加动态到这些东西上去,表现动态的画面。比如移动照相机机。
      

  2.   

    看这里
    http://www.cnblogs.com/alamiye010/archive/2009/08/26/1554539.html