2012-03-23 57 views
2

我有與使用相同的技術XAML數據綁定,對象不匹配目標類型

這對於命名爲新聞源

<!-- 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}}"/> 
第一組頁2的外觀相似類2不同組頁

到該組數據傳遞到該組頁。

這對於命名爲事件

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

即使是能夠在整個應用程序運行良好。但是我發現上面有一個源代碼的底部。藍線顯示對象與目標類型不匹配。

任何knw wat發生的代碼? = D 對不起,如果我沒有發佈所有信息。有點大。如果需要更多信息,請詢問。

+0

你能發佈2件更多的事情。我想我有一個想法..你能不能給我們的XAML線,其中NewsFeedDataSource和EventDataSource是否已設置,並且您是否可以驗證您是否正在(在常規代碼中)設置DefaultViewModel集合(由DataSource使用的集合)中的適當值? – DevTheo 2012-03-23 18:46:21

+0

@DevTheo 您是否創建了窗口8 metro app b4?它與樣本完全一樣,而我只是複製相同的一組類並編輯其中的一些信息。無論如何,因爲你的要求,我嘗試把相關的代碼,以及= D – 2012-03-25 17:32:08

+0

其實,這是我得到的..我遇到了這樣的事情,我有一個樣本。在我的情況下,我有幾件事情是錯誤的..最值得注意的是,我的DefaultViewModel提到了不同的來源..(雖然我也有一些錯誤的項目) – DevTheo 2012-03-26 13:09:54

回答

3

經過幾次更多的測試後,我發現這個問題是由於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>