UWP:DataTemplateSelector 和 SelectedItem
UWP: DataTemplateSelector and SelectedItem
如何更改 ListView 中 SelectedItem 的 DataTemplate [=23] =]UWP 项目?我正在尝试使用 DataTemplateSelector 但问题是 SelectTemplateCore 仅被调用一次。
这是我尝试过的:
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;
}
}
页面资源
<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 x:Name="MyListView" ItemContainerStyle="{StaticResource DefaultStyle}" ItemTemplateSelector="{StaticResource TemplateSelector}">
</ListView>
一种方法是用RegisterPropertyChangedCallback
监测IsSelected
属性的变化,然后手动更新ListViewItem
的ContentTemplateSelector
-
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;
}
如何更改 ListView 中 SelectedItem 的 DataTemplate [=23] =]UWP 项目?我正在尝试使用 DataTemplateSelector 但问题是 SelectTemplateCore 仅被调用一次。
这是我尝试过的:
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;
}
}
页面资源
<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 x:Name="MyListView" ItemContainerStyle="{StaticResource DefaultStyle}" ItemTemplateSelector="{StaticResource TemplateSelector}">
</ListView>
一种方法是用RegisterPropertyChangedCallback
监测IsSelected
属性的变化,然后手动更新ListViewItem
的ContentTemplateSelector
-
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;
}