WPF错误“缺少对象的键值

时间:2012-02-29 19:24:27

标签: xaml exception key datatemplate

我在build上的xaml资源文件中收到错误。这是xaml文件:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="clr-namespace:DiagramDesigner"
                    xmlns:c="clr-namespace:DiagramDesigner.Controls"
                    xmlns:r="clr-namespace:Automation.Data;assembly=Automation"
                    xmlns:a="clr-namespace:Automation;assembly=Automation"  >
    <DataTemplate x:Key="WorkflowDropdownSetting">
        <DataTemplate.Resources>
            <s:WorkflowDropdown x:Key="settingFlowList" />
        </DataTemplate.Resources>
        <StackPanel Orientation="Horizontal">
            <ComboBox ItemsSource="{StaticResource settingFlowList}" Width="200" SelectedValue="{Binding Path=DefaultValue, Mode=TwoWay}" DisplayMemberPath="Name" SelectedValuePath="Id"/>
            <Button x:Name="btnEditWorkflow" Command="{x:Static s:WorkflowUICommands.EditWorkflow}" CommandParameter="{Binding Path=DefaultValue}">Edit...</Button>
        </StackPanel>
    </DataTemplate>
</ResourceDictionary>

这是WorkflowList.cs的源代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Automation;
using System.Collections.ObjectModel;
using Automation.Data;

namespace DiagramDesigner
{
    public class WorkflowList : ObservableCollection<Workflow>
    {
        public WorkflowList()
        {
            AutomationDataContext cntxt = Engine.Data;
            IEnumerable<Workflow> lst = Engine.Data.Workflows.AsEnumerable<Workflow>();

            foreach (Workflow flow in lst)
            {
                Add(flow);
            }
        }
    }
}

我在另一个xaml(不是资源文件)中使用此对象作为资源,它可以正常工作:

<Window x:Class="DiagramDesigner.WorkflowMain"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:s="clr-namespace:DiagramDesigner"
     Title="WorkflowMain" SizeToContent="Width" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <ScrollViewer Grid.IsSharedSizeScope="True">
        <ScrollViewer.Resources>
            <ResourceDictionary>
                <s:WorkflowList x:Key="flows"/>
                <Brush x:Key="BrdrBrush">#D6FF9436</Brush>
                <Style TargetType="TextBlock">
                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    <Setter Property="VerticalAlignment" Value="Stretch"/>
                </Style>
                <Style TargetType="WrapPanel">
                    <Setter Property="Margin" Value="2"/>
                    <Setter Property="Background" Value="PowderBlue"/>
                    <Setter Property="VerticalAlignment" Value="Stretch"/>
                </Style>
                <Style x:Key="Heading" TargetType="TextBlock">
                    <Setter Property="FontSize" Value="20"/>
                    <Setter Property="FontWeight" Value="bold"/>
                </Style>
                <Style x:Key="ColumnHeader" TargetType="Label">
                    <Setter Property="FontWeight" Value="bold"/>
                </Style>
            </ResourceDictionary>
        </ScrollViewer.Resources>
        <Border BorderThickness="1"
            Padding="4"
            BorderBrush="{StaticResource BrdrBrush}"
            Background="AliceBlue"
            SnapsToDevicePixels="True" HorizontalAlignment="Stretch">
            <StackPanel>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition SharedSizeGroup="Job"></ColumnDefinition>
                        <ColumnDefinition SharedSizeGroup="Name"></ColumnDefinition>
                        <ColumnDefinition SharedSizeGroup="Description"></ColumnDefinition>
                        <ColumnDefinition SharedSizeGroup="FirstAction"></ColumnDefinition>
                        <ColumnDefinition SharedSizeGroup="Button"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <WrapPanel Grid.Row="0" Grid.ColumnSpan="5">
                        <TextBlock Text="Workflows:" Style="{StaticResource Heading}"/>
                    </WrapPanel>
                    <WrapPanel Grid.Column="0" Grid.Row="1">
                        <Label Style="{StaticResource ColumnHeader}">Name:</Label>
                    </WrapPanel>
                    <WrapPanel Grid.Column="1" Grid.Row="1">
                        <Label Style="{StaticResource ColumnHeader}">Name:</Label>
                    </WrapPanel>
                    <WrapPanel Grid.Column="2" Grid.Row="1">
                        <Label Style="{StaticResource ColumnHeader}">Description:</Label>
                    </WrapPanel>
                    <WrapPanel Grid.Column="3" Grid.Row="1">
                        <Label Style="{StaticResource ColumnHeader}">First Task:</Label>
                    </WrapPanel>
                    <WrapPanel Grid.Column="4" Grid.Row="1">
                    </WrapPanel>
                </Grid>
                <ItemsControl ItemsSource="{Binding Source={StaticResource flows}}" Grid.Row="2" Grid.ColumnSpan="5" HorizontalAlignment="Stretch">
                    <ItemsControl.Template>
                        <ControlTemplate>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                        </ControlTemplate>
                    </ItemsControl.Template>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Margin="0,5,0,5"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <DataTemplate.Resources>
                                <Style TargetType="{x:Type WrapPanel}">
                                    <Setter Property="Margin" Value="2"/>
                                    <Setter Property="Background" Value="PowderBlue"/>
                                    <Setter Property="MinHeight" Value="30" />
                                    <Setter Property="VerticalAlignment" Value="Stretch"/>
                                </Style>
                            </DataTemplate.Resources>
                            <Grid Background="AliceBlue"  Margin="0 0 0 5"  HorizontalAlignment="Stretch">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition SharedSizeGroup="Job"></ColumnDefinition>
                                    <ColumnDefinition SharedSizeGroup="Name"></ColumnDefinition>
                                    <ColumnDefinition SharedSizeGroup="Description"></ColumnDefinition>
                                    <ColumnDefinition SharedSizeGroup="FirstAction"></ColumnDefinition>
                                    <ColumnDefinition SharedSizeGroup="Button"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <WrapPanel>
                                    <TextBlock Text="{Binding Path=Name}" Grid.Column="1"/>
                                </WrapPanel>
                                <WrapPanel>
                                    <TextBlock Text="{Binding Path=Job.Name}" Grid.Column="0"/>
                                </WrapPanel>
                                <WrapPanel Grid.Column="2">
                                    <TextBlock Text="{Binding Path=Description}"/>
                                </WrapPanel>
                                <WrapPanel Grid.Column="3">
                                    <TextBlock Text="{Binding Path=FirstAction.Name}"/>
                                </WrapPanel>
                                <WrapPanel Grid.Column="4">
                                    <Button Name="editButton" Command="{x:Static s:WorkflowUICommands.EditWorkflow}" CommandParameter="{Binding}" IsDefault="True" VerticalAlignment="Stretch">Edit...</Button>
                                </WrapPanel>
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
                <Button Name="btnNewWorkflow" Command="{x:Static s:WorkflowUICommands.NewWorkflow}" >New Workflow...</Button>
            </StackPanel>
        </Border>
    </ScrollViewer>
</Window>

当我构建时,我看到了这个错误:

Error   5   Missing key value on 'WorkflowDropdown' object. C:\Source\Projects\devtest\UI\WorkflowUI\DiagramDesigner\Resources\SettingTemplates.xaml    7   2   DiagramDesigner

The only question我发现这个错误不适用,因为我已经使用x:Key了。

解决方案运行得很好,但错误是阻止设计人员进行交互。它会显示正常,直到您尝试与它进行交互,然后通过顶部列出的以下异常禁用它:

XamlObjectWriterException was thrown on "DataTemplate":Missing key value on 'WorkflowList' object. Click here to hide detail.

An Unhandled Exception has occurred
Missing key value on 'WorkflowList' object.
at System.Xaml.XamlObjectWriter.GetKeyFromInstance(Object instance, XamlType instanceType, IAddLineInfo lineInfo)
at System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentCollection(ObjectWriterContext ctx)
at System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx)
at System.Xaml.XamlObjectWriter.WriteEndObject()
at System.Xaml.XamlWriter.WriteNode(XamlReader reader)
at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)

任何帮助都会非常感激,因为我似乎只是在转动我的车轮。

1 个答案:

答案 0 :(得分:0)

我没有答案,但可以说设计师在自定义类,属性等方面非常敏感和不稳定。特别是当它们位于项目的子文件夹中时。