WPF - 绑定到Listview项目中的列表

时间:2010-05-10 18:57:07

标签: c# wpf data-binding listview

所以我试图绑定到ListView项目中的列表,但我似乎无法使绑定正确。如果有人可以帮助我修正那些很棒的装订!

以下是您可能需要的来源:

//class that xaml is initially bound to
public partial class UploadMngPanel : Grid
{
    ....
    //initial list to bind to
    public ObservableCollection<FinishedAnimeCollection> UploadedAnime
    {
        get { return uploadedAnime; }
    }
}

public class FinishedAnimeCollection
{
    ...
    //second list to bind to
    private ObservableCollection<AnimeEpisodeItem> _episodes = new ObservableCollection<AnimeEpisodeItem>();

    public ObservableCollection<AnimeEpisodeItem> Episodes
    {
        get { return _episodes; }
    }
 }

public class AnimeEpisodeItem
{
    public String Title { get; set; }

    public DateTime TimeAdded { get; set; }
}

我想修复的XAML在

之下
<!-- First list binding here (this works)-->
<ListView Name="finishedView" ItemsSource="{Binding UploadedAnime}">
 <ListView.Resources>
  <ResourceDictionary>
   <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="AnimeExpander.xaml"/>
   </ResourceDictionary.MergedDictionaries>

   <DataTemplate x:Key="AnimeRow">
    <DockPanel>
     <!-- <Image Height="75" Width="Auto" Source="{Binding Image}" DockPanel.Dock="Left" VerticalAlignment="Top"/> -->
     <Expander Template="{StaticResource AnimeExpanderControlTemplate}" Header="{Binding AnimeTitle}">
      <Expander.ContentTemplate>
       <DataTemplate>
        <Border BorderBrush="Black" BorderThickness="1,1,1,1">
         <!--Second list binding here (this doesn't work)-->
         <ListView ItemsSource="{Binding Path=Episodes}">
          <ListViewItem>
           <DockPanel>
            <TextBlock Text="{Binding Title}" DockPanel.Dock="Left" />
            <!--<TextBlock Text="{Binding TimeAdded}" DockPanel.Dock="Right" />-->
           </DockPanel>
          </ListViewItem>              
         </ListView>
        </Border>
       </DataTemplate>
      </Expander.ContentTemplate>
     </Expander>
    </DockPanel>       
   </DataTemplate>        
  </ResourceDictionary>     
 </ListView.Resources>

 <ListView.View>
  <GridView>
   <GridViewColumn Width="700" Header="Anime" CellTemplate="{StaticResource AnimeRow}"/>       
  </GridView>
 </ListView.View>

</ListView>

如果您需要更多源代码,请告诉我们。非常感谢!

1 个答案:

答案 0 :(得分:1)

好的,经过一段时间的愚弄,我终于找到了怎么做。显然这个:

 <ListView ItemsSource="{Binding Path=Episodes}">
       <ListViewItem>
          <DockPanel>
             <TextBlock Text="{Binding Title}" DockPanel.Dock="Left" />
             <!--<TextBlock Text="{Binding TimeAdded}" DockPanel.Dock="Right" />-->
          </DockPanel>
        </ListViewItem>                                                     
  </ListView>

即使没有通过并且错误也无效。为列表视图指定ItemSource时,不能在ListView中使用ListViewItem标记。所以我将我的代码重新编写为以下代码:

<TabItem Header="Finished">
            <TabItem.Resources>
                <ResourceDictionary>
                        <ResourceDictionary.MergedDictionaries>
                            <ResourceDictionary Source="AnimeExpander.xaml"/>
                        </ResourceDictionary.MergedDictionaries>

                        <DataTemplate x:Key="EpisodeItem">
                            <DockPanel Margin="30,3">
                                <TextBlock Text="{Binding Title}" DockPanel.Dock="Left" />
                                <WrapPanel Margin="10,0" DockPanel.Dock="Right">
                                    <TextBlock Text="Finished at: " />
                                    <TextBlock Text="{Binding TimeAdded}" />
                                </WrapPanel>
                            </DockPanel>
                        </DataTemplate>

                        <DataTemplate x:Key="AnimeItem">
                            <DockPanel Margin="5,10">
                                <Image Height="75" Width="Auto" Source="{Binding ImagePath}" DockPanel.Dock="Left" VerticalAlignment="Top"/> 
                                <Expander Template="{StaticResource AnimeExpanderControlTemplate}" >
                                    <Expander.Header>
                                        <TextBlock FontWeight="Bold" Text="{Binding AnimeTitle}" />
                                    </Expander.Header>

                                        <ListView ItemsSource="{Binding Episodes}" ItemTemplate="{StaticResource EpisodeItem}" BorderThickness="0,0,0,0" />

                                </Expander>
                            </DockPanel>                            
                        </DataTemplate>                         
                    </ResourceDictionary>           
            </TabItem.Resources>

            <ListView Name="finishedView" ItemsSource="{Binding UploadedAnime, diagnostics:PresentationTraceSources.TraceLevel=High}" ItemTemplate="{StaticResource AnimeItem}" />                  
</TabItem>