gpt4 book ai didi

c# - XAML 数据绑定(bind),对象与目标类型不匹配

转载 作者:行者123 更新时间:2023-11-30 22:30:08 25 4
gpt4 key购买 nike

我有 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}}"/>

将群组数据传递到群组页面。

这用于命名为事件的第二组页面

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

和上面的ViewSource绑定(bind)的部分代码示例

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

和绑定(bind)到 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对不起,如果我没有发布所有信息。有点大。如果需要更多信息,请索取。

最佳答案

经过几次测试,我发现这个问题是由于 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>

关于c# - XAML 数据绑定(bind),对象与目标类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9837329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com