Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

c# - UWP: DataTemplateSelector and SelectedItem

How can I change the DataTemplate for the SelectedItem of ListView in UWP project? I'm trying to use the DataTemplateSelector but the problem is that SelectTemplateCore is called only one time.

This what I have tried:

MyTemplateSelector class

public class MyTemplateSelector : DataTemplateSelector
{
    public DataTemplate DefaultTemplate { get; set; }
    public DataTemplate SelectedItemTemplate { get; set; }
    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        var cont = container as ListViewItem;
        if (cont != null)
        {
            FrameworkElement elemnt = container as FrameworkElement;
            if (cont.IsSelected)
            {
                return SelectedItemTemplate;
            }
            else
            {
                return DefaultTemplate;
            }
        }
        else
            return DefaultTemplate;
    }
}

Resources of the page

<Page.Resources>
    <DataTemplate x:Key="DefaultTemplate">
        <Grid Height="44">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="44" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <SymbolIcon Grid.Column="0" Symbol="{Binding Icon, Mode=OneWay}" Foreground="Black"
                        Height="40" Width="40" Margin="2"/>
            <TextBlock Grid.Column="1" Text="{Binding Name, Mode=OneWay}" FontSize="16" VerticalAlignment="Center" Foreground="Black" />
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="SelectedItemTemplate">
        <Grid Height="44" Background="Black">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="44" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <SymbolIcon Grid.Column="0" Symbol="{Binding Icon, Mode=OneWay}" Foreground="Black"
                        Height="40" Width="40" Margin="2"/>
            <TextBlock Grid.Column="1" Text="{Binding Name, Mode=OneWay}" FontSize="16" VerticalAlignment="Center" Foreground="Black" />
        </Grid>
    </DataTemplate>
    <local:DefaultTemplateSelector x:Key="TemplateSelector"
                                   DefaultTemplate="{StaticResource DefaultTemplate}"
                                   SelectedItemTemplate="{StaticResource SelectedItemTemplate}"/>
</Page.Resources>

ListView

<ListView x:Name="MyListView" ItemContainerStyle="{StaticResource DefaultStyle}" ItemTemplateSelector="{StaticResource TemplateSelector}">
</ListView>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One way is to use RegisterPropertyChangedCallback to monitor the changes of the IsSelected property and then update the ContentTemplateSelector of the ListViewItem manually -

protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
    if (container is ListViewItem cont)
    {
        if (cont.Tag != null && long.TryParse(cont.Tag.ToString(), out var token))
        {
            cont.UnregisterPropertyChangedCallback(ListViewItem.IsSelectedProperty, token);
        }

        cont.Tag = cont.RegisterPropertyChangedCallback(ListViewItem.IsSelectedProperty, (s, e) =>
        {
            cont.ContentTemplateSelector = null;
            cont.ContentTemplateSelector = this;
        });

        if (cont.IsSelected)
        {
            return SelectedItemTemplate;
        }

        return DefaultTemplate;
    }

    return DefaultTemplate;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...