C# WPF IDictionary编译错误
我的窗口中有此数据模板。资源:C# WPF IDictionary编译错误,c#,wpf,C#,Wpf,我的窗口中有此数据模板。资源: <DataTemplate x:Key="InterfacesDataTemplate" DataType="ca:Interface"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> </Grid.RowDefi
<DataTemplate x:Key="InterfacesDataTemplate"
DataType="ca:Interface">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1"
Text="{Binding Path=Name}" />
</Grid>
</DataTemplate>
我犯了个错误
添加到IDictionary的所有对象都必须具有键属性或与之相关联的其他类型的键
但是我有x:Key属性,所以我不知道出了什么问题
编辑:这是所有窗口。参考资料
<Window.Resources>
<DataTemplate x:Key="InterfacesDataTemplate"
DataType="ca:Interface">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1"
Text="{Binding Path=Name}" />
</Grid>
</DataTemplate>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--
Merge in the resource dictionary that is shared between the main window and the overview window.
-->
<ResourceDictionary
Source="SharedVisualTemplates.xaml"
/>
</ResourceDictionary.MergedDictionaries>
<!-- UI commands. -->
<RoutedUICommand x:Key="Commands.DeleteSelectedNodes" />
<RoutedUICommand x:Key="Commands.CreateNode" />
<RoutedUICommand x:Key="Commands.DeleteNode" />
<RoutedUICommand x:Key="Commands.DeleteConnection" />
<RoutedUICommand x:Key="Commands.ZoomOut" />
<RoutedUICommand x:Key="Commands.ZoomIn" />
<RoutedUICommand x:Key="Commands.JumpBackToPrevZoom" />
<RoutedUICommand x:Key="Commands.FitContent" />
<RoutedUICommand x:Key="Commands.Fill" />
<RoutedUICommand x:Key="Commands.OneHundredPercent" />
<!--
This converts from a scale value to a percentage value.
It is used to convert the value of 'ContentScale' to the percentage zoom level that is displayed in the UI.
-->
<con:ScaleToPercentConverter
x:Key="scaleToPercentConverter"
/>
<!--
Define the visual style for a 'ConnectorItem'.
-->
<Style
TargetType="{x:Type NetworkUI:ConnectorItem}"
>
<!--
Data-binding for the connector hotspot.
ConnectorItem automatically computes its center points and assings this value
to the 'Hotspot' property. This data-binding then 'pushes' the value into the application
view-model.
-->
<Setter
Property="Hotspot"
Value="{Binding Hotspot, Mode=OneWayToSource}"
/>
<!-- The visual template. -->
<Setter
Property="Template"
>
<Setter.Value>
<ControlTemplate
TargetType="{x:Type NetworkUI:ConnectorItem}"
>
<!-- The visual for the connector. -->
<Ellipse
Stroke="{StaticResource nodeBorderBrush}"
Fill="{StaticResource connectorBackgroundBrush}"
/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--Style For MainListBox-->
<Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Header="{Binding Name}" IsExpanded="True">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!--<Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Header="Interfaces" IsExpanded="True">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>-->
<!--
Template for the button that is used to delete nodes and connections in the graph.
This button is displayed in an adorner when the user hovers the mouse over a node or connection.
-->
<ControlTemplate
x:Key="deleteButtonTemplate"
TargetType="{x:Type Button}"
>
<Grid
x:Name="grid"
>
<Grid.RenderTransform>
<ScaleTransform
ScaleX="1"
ScaleY="1"
CenterX="10"
CenterY="10"
/>
</Grid.RenderTransform>
<Ellipse
x:Name="shadow"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Fill="Gray"
>
<Ellipse.RenderTransform>
<TranslateTransform
X="1.5"
Y="1.5"
/>
</Ellipse.RenderTransform>
</Ellipse>
<Ellipse
x:Name="ellipse"
Stroke="Black"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Fill="White"
/>
<Image
Source="Resources\scissors.png"
Margin="2"
/>
</Grid>
<ControlTemplate.Triggers>
<EventTrigger
RoutedEvent="Mouse.MouseEnter"
>
<!--
Make the 'delete connection button' larger when the mouse
cursor is hovered over it.
-->
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="grid"
Storyboard.TargetProperty="RenderTransform.ScaleX"
To="1.3"
Duration="0:0:0.25"
/>
<DoubleAnimation
Storyboard.TargetName="grid"
Storyboard.TargetProperty="RenderTransform.ScaleY"
To="1.3"
Duration="0:0:0.25"
/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger
RoutedEvent="Mouse.MouseLeave"
>
<!--
Return the 'delete connection button' to normal size when the mouse
cursor is moved away.
-->
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="grid"
Storyboard.TargetProperty="RenderTransform.ScaleX"
To="1"
Duration="0:0:0.05"
/>
<DoubleAnimation
Storyboard.TargetName="grid"
Storyboard.TargetProperty="RenderTransform.ScaleY"
To="1"
Duration="0:0:0.05"
/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!--
Data-template for ConectionViewModel.
Note that the 'Start' and 'End' of the arrow are bound to 'SourceConnectorHotspot' and 'DestConnectorHotspot' in
the view-model.
In this sample a curved arrow represents connections between nodes.
-->
<DataTemplate
DataType="{x:Type local:ConnectionViewModel}"
>
<!-- The connection is represented by a curved arrow. -->
<ca:CurvedArrow
StrokeThickness="2"
Points="{Binding Points}"
Fill="{StaticResource connectionBrush}"
Stroke="{StaticResource connectionBrush}"
/>
<!--Fill="{Binding isFlow}"-->
<!--Stroke="{Binding isFlow}"-->
<!--
An adorned control is used, to represent the connection.
When the user hovers the mouse cursor over the connection, the
'delete connection' adorner pops up and allows them to delete the connection.
-->
</DataTemplate>
<!-- Define a data-template for the 'NodeViewModel' class. -->
<DataTemplate
DataType="{x:Type local:NodeViewModel}"
>
<!-- The margin has been selected so that the selection rect nicely covers the entire node. -->
<Grid
MinWidth="120"
Margin="10,6,10,6"
SizeChanged="Node_SizeChanged"
>
<!-- This rectangle is the main visual for the node. -->
<Rectangle
Stroke="{StaticResource nodeBorderBrush}"
StrokeThickness="1.3"
RadiusX="4"
RadiusY="4"
Fill="{StaticResource nodeFillBrush}"
/>
<!--
This grid contains the node's connectors.
The margin is negative so that the connectors overlap the body of the node and it's selection border.
-->
<Grid
Margin="-6,4,-6,4"
>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" MinWidth="10" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<!-- spacer -->
<RowDefinition Height="2" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- The name of the node. -->
<TextBlock
Grid.Column="0"
Grid.ColumnSpan="3"
Grid.Row="0"
Text="{Binding Name}"
HorizontalAlignment="Center"
VerticalAlignment="Center"
/>
<!-- Displays the node's input connectors. -->
<ItemsControl
Grid.Column="0"
Grid.Row="2"
ItemsSource="{Binding InputConnectors}"
ItemTemplate="{StaticResource inputConnectorTemplate}"
Focusable="False"
/>
<!-- Displays the node's output connectors. -->
<ItemsControl
Grid.Column="2"
Grid.Row="2"
ItemsSource="{Binding OutputConnectors}"
ItemTemplate="{StaticResource outputConnectorTemplate}"
Focusable="False"
/>
<ListBox Name="lbInterfaces" Background="Transparent" ItemsSource="{Binding Interfaces}" ItemTemplate="InterfacesDataTemplate" >
<ListBox.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/>
</ListBox.GroupStyle>
</ListBox>
<ListBox Name="lbEnums" Background="Transparent" ItemsSource="{Binding Enums}">
<ListBox.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="Enums"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Name="lbStructs" Background="Transparent" ItemsSource="{Binding Structs}">
<ListBox.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="Structs"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
这是因为您的
是在资源字典
之前声明的。您必须在ResourceDictionary
的中或之后声明它
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--
Merge in the resource dictionary that is shared between the main window and the overview window.
-->
<ResourceDictionary Source="SharedVisualTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="InterfacesDataTemplate" DataType="ca:Interface">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1"
Text="{Binding Path=Name}" />
</Grid>
</DataTemplate>
// Other resources
</ResourceDictionary>
//其他资源
或
//其他资源
它在这里起作用。你能给出窗口的所有内容吗。参考资料
?是的,但它确实很大,我不能把它放在评论中。你确定没有其他资源没有键
@Yogevnn你可以编辑你的问题。@Yogevnn你可以用它编辑你的问题。哦,我不知道,我现在就编辑它。谢谢大家,在同一个代码中,我现在遇到了一个异常:无法将类型为“System.String”的对象强制转换为类型为“System.Windows.DataTemplate”。@Yogevnn在使用数据模板中的键时是否使用了StaticResource
?例如:Template=“{StaticResource key}”
不,我没有:@Yogevnn不,我是说在哪里使用DataTemplate
。所以不是在Windows.Resources
,而是在一个UI元素上。哦,对不起。在这里:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!--
Merge in the resource dictionary that is shared between the main window and the overview window.
-->
<ResourceDictionary Source="SharedVisualTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
// Other resources
</ResourceDictionary>
<DataTemplate x:Key="InterfacesDataTemplate" DataType="ca:Interface">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1"
Text="{Binding Path=Name}" />
</Grid>
</DataTemplate>