none
WPF中Path怎么添加多边形,后台代码根据point数组添加 RRS feed

  • 问题

  • WPF中Path怎么添加多边形,后台代码根据point数组添加

    如:


    <Path Data="M 255.30 150.90 L 181.80 179.20 182.00 258.00 132.35 
    196.80 57.50 221.30 100.35 155.20 53.90 91.60 130.00 111.90 
    176.15 48.10 180.35 126.75 255.30 150.90 ">
    </Path>


    2012年6月15日 7:00

全部回复

  • 你可以binding point属性,比如这样:

    <Polygon x:Name="shape"
    				 StrokeThickness="1.5" 
    				 Fill="Blue"
    				 Stroke="Blue"
    				 Points="{Binding Path=Points, Converter={StaticResource converter}}" />

    我这里有一个关于Point TwoWay绑定的例子,

    	<Window.Resources>
    		<local:PolygonItem x:Key="src"/>
    
    		<local:Converter x:Key="converter" />
    
    		<DataTemplate DataType="{x:Type local:PolygonItem}">
    			<Polygon x:Name="shape"
    				 StrokeThickness="1.5" 
    				 Fill="Blue"
    				 Stroke="Blue"
    				 Points="{Binding Path=Points, Converter={StaticResource converter}}" />
    		</DataTemplate>
    
    	</Window.Resources>
    
    	<StackPanel>
    		<ContentControl Content="{Binding Source={StaticResource src}}" />
    		<Button Click="ChangeSource" Width="150" Margin="20">Change data source</Button>
    	</StackPanel>
    	public partial class Window1 : System.Windows.Window
    	{
    
    		public Window1()
    		{
    			InitializeComponent();
    		}
    
    		private void ChangeSource(object sender, RoutedEventArgs e)
    		{
    			PolygonItem polygon = this.Resources["src"] as PolygonItem;
    			polygon.Add(new MyPoint(150, 70));
    		}
    	}
    
    	public class PolygonItem : INotifyPropertyChanged
    	{
    		public event PropertyChangedEventHandler PropertyChanged;
    
    		protected virtual void OnPropertyChanged(string propertyName)
    		{
    			if (this.PropertyChanged != null)
    			{
    				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    			}
    		}
    
    		private List<MyPoint> _points = new List<MyPoint>();
    
    		public ReadOnlyCollection<MyPoint> Points
    		{
    			get { return  new ReadOnlyCollection<MyPoint>(_points); }
    			set
    			{
    				_points = new List<MyPoint>(value);
    				OnPropertyChanged("Points");
    			}
    		}
    
    		public void Add(MyPoint point)
    		{
    			this._points.Add(point);
    			OnPropertyChanged("Points");
    		}
    
    		public PolygonItem()
    		{
    			_points.Add(new MyPoint(100, 50));
    			_points.Add(new MyPoint(50, 100));
    			_points.Add(new MyPoint(150, 50));
    		}
    	}
    
    	public class MyPoint
    	{
    		private double x;
    
    		public double X
    		{
    			get { return x; }
    		}
    
    		private double y;
    
    		public double Y
    		{
    			get { return y; }
    		}
    
    		public MyPoint(double x, double y)
    		{
    			this.x = x;
    			this.y = y;
    		}
    	}
    
    	public class Converter : IValueConverter
    	{
    		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    		{
    			ReadOnlyCollection<MyPoint> collection = value as ReadOnlyCollection<MyPoint>;
    			PointCollection points = new PointCollection();
    			foreach (MyPoint myPoint in collection)
    			{
    				points.Add(new Point(myPoint.X, myPoint.Y));
    			}
    
    			return points;
    		}
    
    		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    		{
    			throw new NotSupportedException("ConvertBack should never be called");
    		}
    	}

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年6月18日 9:34
    版主