如果你能接受定义X,Y,在你的模型,你可以用小病急乱投医的TreeView做到这一点:
这里是XAML中:
<TreeView ItemsSource="{Binding ComponentsToDraw}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:Component}" ItemsSource="{Binding ComponentsToDraw}">
<Grid Canvas.Left="{Binding X}" Canvas.Top="{Binding Y}" Width="{Binding Width}" Height="{Binding Height}" Background="{Binding Color}">
<TextBlock Margin="8,5,0,0" FontWeight="Bold" Text="{Binding Name}" />
</Grid>
</HierarchicalDataTemplate>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<Canvas>
<ContentPresenter x:Name="PART_Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header">
<ContentPresenter.Style>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Path=X}" />
<Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
</Style>
</ContentPresenter.Style>
</ContentPresenter>
<ItemsPresenter/>
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
你这里的ItemTemplate风格,makde ContentPresenter作为Canvas和HierarichalDataTemplate,使您的dataTemplate看起来像带有一些文本的Rectangle。
这里是模型我用:
public class Component
{
public Component() { }
public ObservableCollection<Component> ComponentsToDraw { set; get; } = new ObservableCollection<Component>();
public double X { set; get; }
public double Y { set; get; }
public double Width { set; get; }
public double Height { set; get; }
public Brush Color { set; get; }
public string Name { set; get; }
}
而且,在视图模型,也使你的控制,找你写的东西,如:
var Solution = new Component() { X = 0, Y = 0, Height = 300, Width = 500, Name = "Solution", Color = new SolidColorBrush(Colors.White)};
var PC = new Component() { X = 0, Y = 0, Height = 300, Width = 245, Name = "PC", Color = new SolidColorBrush(Colors.LightBlue) };
var PC2 = new Component() { X = 255, Y = 0, Height = 195, Width = 245, Name = "PC2", Color = new SolidColorBrush(Colors.LightBlue) };
var SubDrawingComponent = new Component() { X = 50, Y = 50, Height = 200, Width = 150, Name = "Drawing Component", Color = new SolidColorBrush(Colors.GreenYellow) };
var DrawingComponent = new Component() { X = 255, Y = 205, Height = 42.5, Width = 245, Name = "Drawing Component", Color = new SolidColorBrush(Colors.GreenYellow) };
var DrawingComponent2 = new Component() { X = 255, Y = 257.5, Height = 42.5, Width = 200, Name = "Drawing Component2", Color = new SolidColorBrush(Colors.GreenYellow) };
Solution.ComponentsToDraw.Add(PC);
Solution.ComponentsToDraw.Add(PC2);
Solution.ComponentsToDraw.Add(DrawingComponent);
Solution.ComponentsToDraw.Add(DrawingComponent2);
PC.ComponentsToDraw.Add(SubDrawingComponent);
ComponentsToDraw.Add(Solution);
对于每个项目在Drawing Component
,你宣告X和Y,这是相对于它的父母。 下面是它的外观:
我知道,我写了一些领域与PascalCase,它惋惜:d
如何设置X,Y?全球或相对? – sTrenat
UserControl可能更适合这里,因为您可以更好地控制它。从Canvas的StackPanel开始,并将每个嵌套项目添加到其父画布。 – Lennart
@sTrenat如果我可以让这棵树结构起作用,我就会考虑亲戚。其他全球也可以工作,我猜 – smok