UWP: DataTemplateSelector and SelectedItem

与世无争的帅哥 提交于 2019-12-29 06:29:14

问题


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>

回答1:


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


来源:https://stackoverflow.com/questions/44249232/uwp-datatemplateselector-and-selecteditem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!