Я сейчас использую следующее решение, предложенное Себастьяном на форуме Microsoft Surface Development:
XAML:
<s:SurfaceWindow
x:Class="Lines.SurfaceWindow1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="http://schemas.microsoft.com/surface/2008"
Title="Lines"
>
<Grid>
<Canvas x:Name="LineHost"/>
<s:ScatterView x:Name="ScatterView"/>
</Grid>
</s:SurfaceWindow>
Кодовый код:
private void BindLineToScatterViewItems(Line line, ScatterViewItem origin,
ScatterViewItem destination)
{
// Bind line.(X1,Y1) to origin.ActualCenter
BindingOperations.SetBinding(line, Line.X1Property, new Binding {
Source = origin, Path = new PropertyPath("ActualCenter.X") });
BindingOperations.SetBinding(line, Line.Y1Property, new Binding {
Source = origin, Path = new PropertyPath("ActualCenter.Y") });
// Bind line.(X2,Y2) to destination.ActualCenter
BindingOperations.SetBinding(line, Line.X2Property, new Binding {
Source = destination, Path = new PropertyPath("ActualCenter.X") });
BindingOperations.SetBinding(line, Line.Y2Property, new Binding {
Source = destination, Path = new PropertyPath("ActualCenter.Y") });
}
Тогда, если вы хотите создать линию между двумя ScatterViewItem
с, просто сделайте это:
var origin = new ScatterViewItem();
var destination = new ScatterViewItem();
Line line = new Line { Stroke = Brushes.Black, StrokeThickness = 2.0 };
BindLineToScatterViewItems(line, origin, destination);
ScatterView.Items.Add(origin);
ScatterView.Items.Add(destination);
LineHost.Children.Add(line);