存储路径几何图形并稍后进行更改

本文关键字:路径 几何图形 存储 | 更新日期: 2023-09-27 18:21:42

我正在使用WPF形状来渲染一些几何体。我保存图形的渲染几何体,稍后再添加它们。现在的问题是,我希望有人能够改变几何对象,比如增加形状的高度和宽度。

我知道一种方法,我可以更新字符串,并将其分配回几何对象以更新它。

有什么替代或更好的方法来实现同样的目标吗?

存储路径几何图形并稍后进行更改

椭圆属性的简单示例:

<Path Fill="Gold" Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <EllipseGeometry Center="50,50" RadiusX="{Binding RadiusX}" RadiusY="50" />
  </Path.Data>
</Path>

并且在ViewModel中(如果不使用MVVM设计模式,则为DataContext)定义一个名为RadiusX的属性(使用INotifyPropertyChangedDependencyProperty通知)。现在,当您更改它时,它应该会更新您的几何图形显示。

这也可以用于路径几何:

为此,你将不得不做两件事之一:

1) 具有如上所述的PathGeometry类型的属性,并定期使用它:

<Path Data="{Binding PointsForPath}"/>

2) 有另一个数据结构,以您想要的方式保存您的积分,然后使用转换器,它将获取您的积分并返回PathGeomerty元素:

<Path Data="{Binding Path=PointsForPath, Converter={StaticResource ResourceKey=PointsConverter}}"/>

保存路径:

如果您使用的是SQL Server(或类似的),则可以选择将几何图形存储为表中的特殊列,以获取更多信息:SQL Geomerty

如果您需要关于答案中使用的术语的进一步帮助,请告诉我:

  • ViewModel

  • DataContext

  • Binding

  • CCD_ 10。

您可以使用Xaml序列化。

来自MSDN:

// Create the Xaml.
Button originalButton = new Button();
originalButton.Height = 50;
originalButton.Width = 100;
originalButton.Background = Brushes.AliceBlue;
originalButton.Content = "Click Me";
// Save the Button to a string. 
string savedButton = XamlWriter.Save(originalButton);
// Load the button
StringReader stringReader = new StringReader(savedButton);
XmlReader xmlReader = XmlReader.Create(stringReader);
Button readerLoadButton = (Button)XamlReader.Load(xmlReader);

请注意,Xaml序列化有一些限制,但据我所见,保存和加载几何图形是可以的。