【问题标题】:XAML data binding, object does not match target typeXAML 数据绑定,对象与目标类型不匹配
【发布时间】:2012-03-23 10:09:27
【问题描述】:

我有 2 个不同的组页面,其中有 2 个使用相同技术的相似类

这适用于名为 NewsFeed 的第一组页面

<!-- Collection of grouped items displayed by this page -->
<CollectionViewSource x:Name="groupedItemsViewSource" Source="{Binding Groups}" IsSourceGrouped="true"
    ItemsPath="Items" d:Source="{Binding ItemGroups, Source={d:DesignInstance Type=data:NewsFeedDataSource, IsDesignTimeCreatable=True}}"/>

将组数据传递到组页面。

这用于名为 Event 的第二组页面

<CollectionViewSource x:Name="groupedItemsViewSource" Source="{Binding Groups}" IsSourceGrouped="true"
    ItemsPath="Items" d:Source="{Binding ItemGroups, Source={d:DesignInstance Type=data:EventDataSource, IsDesignTimeCreatable=True}}"/>

以及上面与ViewSource绑定的部分代码示例

 <GridView x:Name="itemGridView" AutomationProperties.AutomationId="ItemGridView" AutomationProperties.Name="Grouped Items" Margin="116,0,40,46"
                ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}" ItemTemplate="{StaticResource Standard250x250ItemTemplate}"
                SelectionMode="None"
                IsItemClickEnabled="True"
                ItemClick="ItemView_ItemClick">

                        <GridView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <VirtualizingStackPanel Orientation="Horizontal"/>
                            </ItemsPanelTemplate>
                        </GridView.ItemsPanel>

                        <GridView.GroupStyle>
                            <GroupStyle>
                                <GroupStyle.HeaderTemplate>
                                    <DataTemplate>
                                        <Grid Margin="1,0,0,6">
                                            <Button
                                        AutomationProperties.Name="Group Title"
                                        Content="{Binding Title}"
                                        Click="Header_Click"
                                        Style="{StaticResource TextButtonStyle}"/>
                                        </Grid>
                                    </DataTemplate>
                                </GroupStyle.HeaderTemplate>

                                <GroupStyle.Panel>
                                    <ItemsPanelTemplate>
                                        <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
                                    </ItemsPanelTemplate>
                                </GroupStyle.Panel>
                            </GroupStyle>
                        </GridView.GroupStyle>
                    </GridView>

绑定到250x250模板代码的静态资源将是

<DataTemplate x:Key="Standard250x250ItemTemplate">
    <Grid HorizontalAlignment="Left" Width="250" Height="250">
        <Border Background="{StaticResource ListViewItemPlaceholderRectBrush}">
            <Image Source="{Binding Image}" Stretch="UniformToFill"/>
        </Border>
        <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundBrush}">
            <TextBlock Text="{Binding Title}" Foreground="{StaticResource ListViewItemOverlayTextBrush}" Style="{StaticResource TitleTextStyle}" Height="60" Margin="15,0,15,0"/>
            <TextBlock Text="{Binding PublishDate}" Foreground="{StaticResource ListViewItemOverlaySecondaryTextBrush}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
        </StackPanel>
    </Grid>
</DataTemplate>

即使整个应用程序都能正常运行。但我发现上面的任一源代码都有一行底部。蓝线表示对象与目标类型不匹配。

有人知道代码发生了什么吗? =D 抱歉,如果我没有发布所有信息。有点大如果需要更多信息,请索取。

【问题讨论】:

  • 你能再发两件事吗.. 我想我有个主意.. 你能告诉我们设置 NewsFeedDataSource 和 EventDataSource 的 XAML 行吗,你能验证你是否正在设置(在常规代码中) DefaultViewModel 集合中的适当值(DataSource 正在使用的那个)?
  • @DevTheo 你创建了window 8 metro app b4吗?它与示例完全相同,而我只是复制同一组类并编辑其中的一些信息。无论如何,根据您的要求,我也尝试将相关代码放入=D
  • 实际上这就是我所得到的......我用我的样本遇到了这样的事情。在我的情况下,我有几件事错了.. 最值得注意的是我的 DefaultViewModel 引用了不同的来源..(尽管我也有一些错误命名的项目)..
  • 因为使用了数据绑定技术,所以名称非常重要,因为数据绑定r指的是组件的名称。因此,虽然来源不同,但我猜名称应该不同。
  • 好吧,想想他们在 WINRT/XAML 中加入了可以绑定多个数据集(视图模型)的能力。这很好,但是当你第一次遇到时真的很困惑它..(再次,我只是确保您绑定到您认为绑定到的内容..)

标签: c# xaml data-binding microsoft-metro winrt-xaml


【解决方案1】:

经过几次测试,我发现这个问题是由于 itemspath 和 d:Source 的名称相同引起的。 代码应该至少看起来像

   <UserControl.Resources>

        <CollectionViewSource x:Name="groupedItemsViewSource" Source="{Binding Groups}" IsSourceGrouped="true"
            ItemsPath="EventItems" d:Source="{Binding EventItemGroups, Source={d:DesignInstance Type=data:EventDataSource, IsDesignTimeCreatable=True}}"/>
    </UserControl.Resources>

<UserControl.Resources>

    <!-- Collection of grouped items displayed by this page -->
    <CollectionViewSource x:Name="groupedItemsViewSource" Source="{Binding Groups}" IsSourceGrouped="true"
        ItemsPath="Items" d:Source="{Binding ItemGroups, Source={d:DesignInstance Type=data:NewsFeedDataSource, IsDesignTimeCreatable=True}}"/>
</UserControl.Resources>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    相关资源
    最近更新 更多