我试图在画布上显示一些框(我自己的 userControl 在其自己的名为 singleNodeControl 的 xaml 文件中定义)并用线连接它们(普通 xaml Line 元素绑定到 LineToParent 类)
这两项都存储在 viewModel 的 Binding 列表中,即<UserControl>
类型。这两个类(框和线)都扩展了 UserControl 类,因此我可以将它们存储到单个绑定列表(canvasNodeSourceList)中
SingleNodeControl 类包含 .cs 文件中的代码以及 .xaml 文件中的模板。
Line Parent 类仅包含 .css 代码,我在其中存储 X1,X2,Y1,Y2 坐标以便稍后绑定它们。
xaml 与 itemsControl 看起来。我认为如果 canvasNodeSourceList 中的项目是 LineToParent 类型,它将使用下面的模板,否则它将使用存储在 singleNodeControl xaml 文件中的模板(更多内容如下)。
但看起来该行的 dataTemplate 并未使用。 SingleNodeCOntrols 被绘制,但线条未被绘制。我缺少什么模板?甚至可以在外部文件中定义一些模板,在项目控件中定义一些模板吗?
我只想显示一些可以通过线连接的框(需要 xaml 定义,因为它们将有多个内部元素)。
<ItemsControl x:Name="content" ItemsSource="{Binding canvasNodeSourceList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate >
<Canvas x:Name="contentCanvas" Background="White" Width="{Binding Width}" Height="{Binding Height}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type slider:LineToParent}">
<!--<Line X1="100" Y1="100" X2="10000" Y2="10000" StrokeThickness="5" Stroke="RED"></Line>-->
<Line X1="{Binding leftPos1}" Y1="{Binding topPos1}" X2="{Binding leftPos2}" Y2="{Binding topPos2}" StrokeThickness="5" Stroke="Black"></Line>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
SingleNodeControl xaml 文件
<UserControl x:Class="WHS_qa.View.SingleNodeControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WHS_qa.View"
mc:Ignorable="d"
>
<Grid>
<Border BorderBrush="Black" BorderThickness="2">
<StackPanel Name="NodeStackPanel">
</StackPanel>
</Border>
</Grid>
我还尝试将 itemsControl 修改为如下所示(将 itemsControl.Resource 更改为 itemsControl.itemTemplate)并显示测试线(包含所有 singleNodeControl 元素)但输出充满错误
<ItemsControl x:Name="content" ItemsSource="{Binding canvasNodeSourceList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate >
<Canvas x:Name="contentCanvas" Background="White" Width="{Binding Width}" Height="{Binding Height}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type slider:LineToParent}">
<Line X1="100" Y1="100" X2="10000" Y2="10000" StrokeThickness="5" Stroke="RED"></Line>
<!--<Line X1="{Binding leftPos1}" Y1="{Binding topPos1}" X2="{Binding leftPos2}" Y2="{Binding topPos2}" StrokeThickness="5" Stroke="Black"></Line>-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
输出错误
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='LineToParent'
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='SingleNodeControl'
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='LineToParent'
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='SingleNodeControl'
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='LineToParent'
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='SingleNodeControl'
System.Windows.Data Error: 26 : ItemTemplate and ItemTemplateSelector are ignored for items already of the ItemsControl's container type; Type='LineToParent'