在WPF中在画布上绘制嵌套元素

时间:2015-03-02 20:18:15

标签: c# wpf xaml

我在画布上显示嵌套元素时遇到问题。 所以我的Datastructure看起来像如下: (Node类是画布上的矩形) Nodelist :(包含一个可观察的NODE集合)        节点:节点包含Connections,这意味着它有类,每个类有两个点:源和目标     我可以在画布上绘制矩形(节点),但我无法在同一画布上绘制线条。     问题在于: - 行代表Node类中的嵌套数据,我在itemstemplate中只有一个datatemplate?     任何人都可以提出建议,我怎么能在画布上显示线条?

Below is the problematic xaml code 

由于

<ItemsControl Name="drawAreaControl" ItemsSource="{Binding NodeList}" >
            <ItemsControl.Resources>
                <DataTemplate DataType="Connection">
                    <Line X1="{Binding Destination.X}" Y1="{Binding Destination.Y}"
                                      X2="{Binding Source.X}" Y2="{Binding Source.Y}"
                                        Stroke="Red" StrokeThickness="3"/>

                </DataTemplate>
            </ItemsControl.Resources>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="FrameworkElement">
                    <Setter Property="Canvas.Left" Value="{Binding CanvasPosition.X}" />
                    <Setter Property="Canvas.Top" Value="{Binding CanvasPosition.Y}" />

                </Style>

            </ItemsControl.ItemContainerStyle>    



            <ItemsControl.ItemTemplate >

                <DataTemplate  DataType="SimpleNode">

                    <ItemsControl ItemsSource="{Binding}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate DataType="Connection">
                                <Canvas><Line X1="{Binding Destination.X}" Y1="{Binding Destination.Y}"
                                      X2="{Binding Source.X}" Y2="{Binding Source.Y}"
                                        Stroke="Red" StrokeThickness="3"/>
                                </Canvas>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>

                    <Rectangle 
                        Uid="{Binding ID}" Name="dropNode"  Width="30"   Height="30"    MouseDown="dropNode_MouseDown" MouseRightButtonDown="dropNode_MouseRightButtonDown" GotFocus="dropNode_GotFocus"  Focusable="True" LostFocus="dropNode_LostFocus" MouseEnter="dropNode_MouseEnter" MouseLeave="dropNode_MouseLeave" >


                    </Rectangle>




                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate >
                    <Canvas IsItemsHost="True" Name="dropCanvas" AllowDrop="True"  Drop="dropcanvas_Drop"  MouseMove="dropCanvas_MouseMove" Grid.ColumnSpan="1" DragEnter="dropCanvas_DragEnter"  Grid.Row="0" Grid.Column="0"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Gray" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>

0 个答案:

没有答案