【发布时间】:2017-09-05 06:46:27
【问题描述】:
我有一个树结构,我在这样的树视图中使用它
根在ObservableCollection<Component> 对象中。 Component 具有 x、y、width、height、children 和 name 属性。我想使用相同的树结构来绘制每个Component 和Rectancle,并在顶部绘制子对象。像这样的
我尝试过这样的 ListBox
<!-- in Resources -->
<DataTemplate DataType="{x:Type m:Component}">
<Border BorderBrush="LightBlue" BorderThickness="1">
<Grid Width="{Binding Width}" Height="{Binding Height}">
<Rectangle Cursor="Hand" Fill="AliceBlue"/>
<Label Content="{Binding Name}" Margin="5"/>
</Grid>
</Border>
</DataTemplate>
<ListBox ItemsSource="{Binding ComponentsToDraw}"
x:Name="listBox"
SelectionMode="Extended"
SelectionChanged="listBox_SelectionChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Background="Transparent"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Canvas.Left" Value="{Binding X}"/>
<Setter Property="Canvas.Top" Value="{Binding Y}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
但是除了我的树的第一层之外,它无法绘制任何东西。
还尝试使用 Treeview,但无法停止绘制我的 Component 对象,例如堆栈面板。
希望任何人都有解决方案或更好的方法。
PS。我没有故意在图纸中包含“解决方案”元素。
【问题讨论】:
-
如何设置 X,Y?全局的还是相对的?
-
UserControl 可能更适合这里,因为您可以更好地控制它。从 Canvases 的 StackPanel 开始,并将每个嵌套项添加到其父 Canvas。
-
@sTrenat 我在考虑是否可以让这个树形结构发挥作用。我猜其他全局也可以工作
标签: c# wpf canvas listbox treeview