Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Binding to a list within a Listview Item

So I am trying to bind to a list within a ListView item but I can't seem to get the binding correct. If some one could help me with the corrected binding that would be great!

Here is the source you will probably need:

//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; }
}

The XAML that I am trying to fix is below

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

If you need any more source code please let me know. Thanks alot!

like image 763
Josh Avatar asked Nov 15 '22 10:11

Josh


1 Answers

Ok so after a bit more fooling around I finally found out how to do it. Apparently this:

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

Is not valid even though it didn't through and error. When you specify an ItemSource for a list view you cannot use the ListViewItem tag within the ListView. So I reworked my code into the following which works:

<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>
like image 75
Josh Avatar answered Dec 20 '22 17:12

Josh