如果我理解你的问题....
我认为您可以采用的方法是使用 DataTemplates、ItemsControl 和 ContentPresentor。
实际上,您想要做的是告诉 WPF “显示”您的视图模型。因为您的视图模型只是普通的类,所以 WPF 不知道如何“渲染”它们。这就是 DataTemplates 的用武之地。这种方法的好处是 DataTemplate 内容的 DataContext 将自动设置为视图模型。 DataTemplates 在您的 Window 或 UserControl 资源中定义:
<Window.Resources>
<DataTemplate DataType="{x:Type ViewModels:SquareViewModel}">
<Views:SquareView />
</DataTemplate>
</Window.Resources>
当 WPF 遇到 SquareViewModel 时,上面的代码将呈现一个 SquareView 用户控件(并将 SquareView 上的 DataContext 设置为 SquareViewModel)。
要将视图模型放置在视图中,您可以使用 ContentPresenter(用于单个 ViewModel):
<ContentPresenter Content="{Binding SingleSquare}" />
在您的情况下,您将希望显示 SquareViewModel 项目的集合,因此您将希望使用 ItemsControl:
<ItemsControl ItemsSource="{Binding Squares}" />
但是,这不会给您想要的结果,因为默认情况下它会像 ListBox 一样。您需要将模板应用到 ItemsControl 以使用基础 Canvas。请参阅this blog by Pete Brown 了解可能的实现。
祝你好运!
编辑:附加代码示例
查看模型:
public class MainViewModel
{
public IEnumerable<SquareViewModel> Squares { get; set; }
public MainViewModel()
{
var squares = new List<SquareViewModel>();
squares.Add(new SquareViewModel(15, 15,100,100, Brushes.CadetBlue, "Square One"));
squares.Add(new SquareViewModel(75,125, 80, 80, Brushes.Indigo, "Square Two"));
Squares = squares;
}
}
public class SquareViewModel
{
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public Brush Color { get; set; }
public string Name { get; set; }
public SquareViewModel(int x, int y, int width, int height, Brush color, string name)
{
X = x;
Y = y;
Width = width;
Height = height;
Color = color;
Name = name;
}
}
观看次数
<UserControl x:Class="MapView.Views.SquareView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Grid Background="{Binding Color, FallbackValue=Azure}">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Name, FallbackValue=None}" />
</Grid>
</UserControl>
<Window x:Class="MapView.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ViewModels="clr-namespace:MapView.ViewModels"
xmlns:Views="clr-namespace:MapView.Views" Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate DataType="{x:Type ViewModels:SquareViewModel}">
<Views:SquareView />
</DataTemplate>
</Window.Resources>
<ItemsControl ItemsSource="{Binding Squares}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Beige" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left"
Value="{Binding X}" />
<Setter Property="Canvas.Top"
Value="{Binding Y}" />
<Setter Property="Width"
Value="{Binding Width}" />
<Setter Property="Height"
Value="{Binding Height}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Window>
在 Window1 构造函数中:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = new MainViewModel();
}
}