WPF IDictionary编译错误

时间:2014-04-23 11:49:00

标签: c# wpf

我的Window.Resources中有这个DataTemplate:

<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的所有对象必须具有Key属性或与其关联的其他类型的键。

但我有x:Key属性,所以我不知道出了什么问题。

编辑:这是所有Window.Resources

    <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>

1 个答案:

答案 0 :(得分:1)

这是因为<DataTemplate x:Key="InterfacesDataTemplate" DataType="ca:Interface">是在ResourceDictionary之前宣布的。您必须在{/ 1}} 之后声明

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>