Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a ListView in WPF. M using TemplateSelector.

XAML code is as
<UserControl Name="asd" x:Class="Quelea.CustomControls.Playlist.Playlist"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:PlayListTemplate="clr-namespace:Quelea.CustomControls.Playlist"
             xmlns:lib="clr-namespace:Quelea.CustomControls.Library"
             mc:Ignorable="d">
    <UserControl.Resources>
        <DataTemplate x:Key="BibleVersTemplate">
            <Grid HorizontalAlignment="Stretch" removed="Aqua">
                <Grid Margin="2,2,2,2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="35" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid Grid.Column="0" VerticalAlignment="Top" HorizontalAlignment="Stretch">
                        <Image Source="../../Resources/images/bible.png" Height="30" VerticalAlignment="Stretch" HorizontalAlignment="Left" />
                    </Grid>
                    <Grid Grid.Column="1">
                        <TextBlock TextWrapping="WrapWithOverflow" Text="{Binding Name}">
                            <TextBlock.ToolTip>
                                <ToolTip>
                                    <StackPanel Orientation="Horizontal">
                                        <Label Visibility="Collapsed" Content="{Binding ResourceId}"></Label>
                                        <TextBlock Text="Book: " FontWeight="Bold" />
                                        <TextBlock Text="{Binding Book}" />
                                        <TextBlock Text="; " />
                                        <TextBlock Text="Chapter: " FontWeight="Bold" />
                                        <TextBlock Text="{Binding Chapter}" />
                                        <TextBlock Text="; " />
                                        <TextBlock Text="Vers: " FontWeight="Bold" />
                                        <TextBlock Text="{Binding Vers}" />
                                    </StackPanel>
                                </ToolTip>
                            </TextBlock.ToolTip>
                        </TextBlock>
                    </Grid>
                </Grid>
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="ImageTemplate">
            <Grid HorizontalAlignment="Stretch" removed="HotPink">
                <Grid Margin="2,2,2,2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="35" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Grid Grid.Column="0" VerticalAlignment="Top" HorizontalAlignment="Stretch">
                        <Image Source="{Binding Path}" Height="30" VerticalAlignment="Stretch" HorizontalAlignment="Left" />
                    </Grid>
                    <Grid Grid.Column="1">
                        <TextBlock TextWrapping="WrapWithOverflow" Text="{Binding Name}">
                            <TextBlock.ToolTip>
                                <ToolTip>
                                    <StackPanel Orientation="Horizontal">
                                        <Label Visibility="Collapsed" Content="{Binding ResourceId}"></Label>
                                        <TextBlock Text="{Binding Name}" />
                                    </StackPanel>
                                </ToolTip>
                            </TextBlock.ToolTip>
                        </TextBlock>
                    </Grid>
                </Grid>
            </Grid>
        </DataTemplate>
        <PlayListTemplate:PlayListTemplateSelector x:Key="myKey"
            BibleVersesTemplate ="{StaticResource BibleVersTemplate}" 
            ImagesTemplate="{StaticResource ImageTemplate}"                                           
        />
    </UserControl.Resources>
    <Grid ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto">
        <Grid.RowDefinitions>
            <RowDefinition Height="26"/>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid Grid.Row="0" Grid.Column="0" VerticalAlignment="Top" HorizontalAlignment="Stretch">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Center">
                    <Label FontWeight="Bold">Playlist</Label>
                </Grid>
                <Grid Grid.Column="1" HorizontalAlignment="Right" Margin="0,0,5,0">
                    <Button Width="20" Height="20" HorizontalAlignment="Right">
                        <Button.Content>
                            <Image Source="../../Resources/images/settings.png"></Image>
                        </Button.Content>
                    </Button>
                </Grid>
            </Grid>
        </Grid>
        <Grid Name="PlayListUI" Grid.Row="1" Grid.Column="0" Margin="0,0,0,0">
            <ListView Name="SchedulesPlayList" AllowDrop="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                      HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                      DragEnter="SchedulesPlayList_DragEnter" Drop="SchedulesPlayList_Drop" SelectionChanged="SchedulesPlayList_SelectionChanged"
                      ItemTemplateSelector="{StaticResource myKey}">
            </ListView>
        </Grid>
    </Grid>
</UserControl>


Template Selected that i used for this is
public class PlayListTemplateSelector : DataTemplateSelector
   {
       public DataTemplate BibleVersesTemplate { get; set; }
       public DataTemplate ImagesTemplate { get; set; }

       public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
       {
           if (item == null)
               return null;

           FrameworkElement frameworkElement = container as FrameworkElement;
           if(frameworkElement != null)
           {
               if (Helpers.DragDropHelper.sourceType == Helpers.DragSourceType.BIBLE)
               {
                   Helpers.DragDropHelper.sourceType = Helpers.DragSourceType.NULL;
                   Helpers.DragDropHelper.sourceEventArgs = null;
                   return BibleVersesTemplate;
               }
               else if (Helpers.DragDropHelper.sourceType == Helpers.DragSourceType.IMAGE)
               {
                   Helpers.DragDropHelper.sourceType = Helpers.DragSourceType.NULL;
                   Helpers.DragDropHelper.sourceEventArgs = null;
                   return ImagesTemplate;
               }
           }

           return base.SelectTemplate(item, container);
       }
   }


The problem is, only one kind of template is active in the ListView at a time.
I want to insert different kind of items in the listView at a time, so that all the type of data templates can be used/active at a time in the list view.

Suppose a listview is having first item as image, second item as Text, Third Item as some other anonymous ClassX object, fourth of type classY object and so on. i.e. The listview is storing different kinds of items at a time.

I have seen, when I am doing this, only one data template type is getting activated at a time for any kind of item.

HOW TO DO THAT? ANY TUTORIAL? OR WORKING CODE? OR HOW TO DO? PLEASE GUIDE!!
Posted
Updated 22-Sep-15 1:39am
v5
Comments
IyyappanCP 12-Oct-15 5:12am    
It seems your logic of PlayListTemplateSelector has problem.its always returns same template. please check that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900