2011-12-04 3 views
4

J'apprends des formes de dessin dans WPF. Je veux dessiner un Path par programme, pas par le biais de XAML. Je ne sais pas ce que l'on peut assigner à la propriété Data.Dessiner un chemin par programme

Path p = new Path(); 
p.Data = ??? 

Répondre

6

Regardez the sample in the MSDN:

//Add the Path Element 
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; 
myGrid.Children.Add(myPath); 

Il est la ligne myPath.Data = myEllipseGeometry; que vous recherchez. Attribuez-lui simplement un objet Geometry.

+0

Merci beaucoup, qui a été très rapide:) ... – Azure

+2

Pas de problème, il était presque aussi vite que la recherche vers le haut sur le MSDN:) –

0

Voici un exemple de quelque chose que je faisais des expériences avec:

using Windows.Foundation; 
using Windows.UI; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Shapes; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Data; 

namespace GridNineExperiment 
{ 

    public class Hamburger : Button 
    { 

     static GeometryCollection DataHamburger = new GeometryCollection 
     { 
      new RectangleGeometry {Rect = new Rect{X = 0, Y = 0, Width = 20, Height = 5 }}, 
      new RectangleGeometry {Rect = new Rect{X = 0, Y = 10, Width = 20, Height = 5 }}, 
      new RectangleGeometry {Rect = new Rect{X = 0, Y = 20, Width = 20, Height = 5 }}, 
     }; 

     static Path PathHamburger = new Path 
     { 
      Fill = new SolidColorBrush(Colors.White), 
      Stroke = new SolidColorBrush(Colors.Black), 
      StrokeThickness = 1.0, 
      Data = new GeometryGroup { Children = DataHamburger } 
     }; 

    public Hamburger() 
    { 
     Content = PathHamburger; 
    } 
} 
Questions connexes