是否可以使用DataTemplate将点集合呈现为一束线(使用数据绑定和拖放)?WPF/Silverlight绑定到视图模型元素集合的画布
下面是详细信息:
我有我的视图模型的多个对象。这些对象最终在绝对像素坐标中指定的画布上具有位置。我需要能够将这些项目拖放到画布上并更新坐标。一些对象由一个点表示,另一些则是线段的集合。我正在使用MVVM(Jounce)。我的视图模型应该公开一个以某种方式绑定坐标的ObservableCollection<Shape>
?那感觉不对。或者有没有一种方法可以在这里使用DataTemplates来绘制每个线段末端的点线,给出一组线段?下面是一个例子视图模型:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Jounce.Core.ViewModel;
namespace CanvasBindTest.ViewModels
{
/// <summary>
/// Sample view model showing design-time resolution of data
/// </summary>
[ExportAsViewModel(typeof(MainViewModel))]
public class MainViewModel : BaseViewModel
{
public MainViewModel()
{
var start = new PointView { X = 0, Y = 0 };
var middle = new PointView { X = 1132/2, Y = 747/2 };
var end = new PointView() { X = 1132, Y = 747 };
var lineView = new LineView(new[] { start, middle, end });
Lines = new LinesView(new[] { lineView });
}
public LinesView Lines { get; private set; }
}
public class LinesView : BaseViewModel
{
public ObservableCollection<LineView> Lines { get; private set; }
public LinesView(IEnumerable<LineView> lines)
{
Lines = new ObservableCollection<LineView>(lines);
}
}
public class LineView : BaseViewModel
{
public ObservableCollection<PointView> Points { get; private set; }
public LineView(IEnumerable<PointView> points)
{
Points = new ObservableCollection<PointView>(points);
}
}
public class PointView : BaseViewModel
{
private int x, y;
public int X
{
get { return x; }
set { x = value; RaisePropertyChanged(() => X); }
}
public int Y {
get { return y; }
set { y = value; RaisePropertyChanged(() => Y); }
}
}
}
这里是视图,其是包装在一个ItemsControl的与背景图像的画布。视图模型坐标是相对于背景图像的非标尺寸:
<UserControl x:Class="CanvasBindTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:CanvasBindTest.ViewModels"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<DataTemplate x:Key="SkylineTemplate" DataType="viewModels:LineView">
<ItemsControl ItemsSource="{Binding Points}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<!--I have a collection of points here, how can I draw all the lines I need and keep the end-points of each line editable?-->
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>
</UserControl.Resources>
<Grid d:DataContext="{d:DesignInstance viewModels:MainViewModel, IsDesignTimeCreatable=True}">
<ScrollViewer x:Name="Scroll">
<ItemsControl ItemsSource="{Binding Lines}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas>
<Canvas.Background>
<ImageBrush Stretch="Uniform" ImageSource="Properties/dv629047.jpg"/>
</Canvas.Background>
</Canvas>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</Grid>
</UserControl>
谢谢您的回复!在我发布的代码中,我有一个'ItemsControl.ItemPanel.ItemTemplate = Canvas',并且我有'ItemsSource'绑定到我的'LinesViewModel'。我google了一下,但没有看到有关'ItemsControl.PanelControl'的更多信息,那是什么?它在Silverlight 5中可用吗?至于'ItemsSource',我不需要将它绑定到'LinesViewModel',它包含所有的行,而这些行又包含所有的点? PointView控件是否是第二个UserControl?第二个XAML文件?或者是数据模板?对不起,我还有点困惑。来自佛罗里达的欢呼! –
我试过嵌套ItemsControls在彼此的内部和使用嵌套的DataTemplate对象。我确实看到我所有的点都是以这种方式绘制的,但是他们都在0,0之上。无论我将每个元素的大小设置为或每个椭圆的Canvas.Top和Canvas.Left属性都没有关系。现在,它看起来像我必须破解它,并暴露我的视图模型中的数据绑定形状。 –
杰森,我编辑了我的答案。 –