2013-07-10 67 views
1

我正在使用WPF形狀來呈現某些幾何圖形。我保存形狀的渲染幾何圖形並稍後添加它們。現在的事情是,我希望有人能夠改變幾何對象,如增加形狀的高度和寬度。存儲路徑幾何並稍後更改

我知道一種方法,我可以更新字符串並將其分配回幾何對象進行更新。

有沒有其他更好的方法來實現?

+0

@ Ron.B.我是... :)我接受了你的回答。謝謝。 –

回答

0

爲橢圓屬性簡單的例子:

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

並在您的視圖模型(或DataContext,如果你不使用MVVM設計模式)定義屬性(無論是法定使用INotifyPropertyChangedDependencyProperty ),名稱爲RadiusX。現在當你改變它時,它應該更新你的幾何顯示。

這也可以爲路徑幾何完成:

對於您將有2件事之一:

1)具有PathGeometry類型的屬性上面的解釋,並經常使用它:

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

2)具有保存你的觀點,你所希望的方式另一種數據結構,然後用一個轉換器,將您的積分,並返回一個PathGeomertyê字元素:

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

爲節省您的路徑:

,如果您使用的是SQL Server(或類似),您可以存儲幾何作爲一個特殊的列在表中,更多的選擇這一信息: SQL Geomerty

讓我知道如果你需要關於在答案中使用的術語進一步的幫助:

  • ViewModel

  • DataContext

  • Binding

  • DependencyProperty

0

您可以使用Xaml序列化。

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

注意,有一個some limitations to Xaml serialization但據我可以看到保存和加載幾何形狀的罰款。