gpt4 book ai didi

wpf - ListView 中第一个和最后一个项目的不同项目模板

转载 作者:行者123 更新时间:2023-12-04 03:13:05 25 4
gpt4 key购买 nike

我需要以不同的方式设置 ListView 的第一项和最后一项的样式。为此,我开始研究基于该答案的解决方案:Use different template for last item in a WPF itemscontrol

基本上,我有一个自定义 ItemsTemplateSelector,它根据 ListView 项目中的项目索引(下面的代码)决定要应用的模板。

它正常工作,除了当列表更新(添加或删除项目)时,模板不会再次被选中(例如,最初,SingleItemTemplate 被选中是因为有一个项目。当我将项目添加到列表中,第一项的模板不会切换到 FirstItemTemplate)。如何强制为所有项目选择模板?

public class FirstLastTemplateSelector : DataTemplateSelector 
{
public DataTemplate DefaultTemplate { get; set; }
public DataTemplate FirstItemTemplate { get; set; }
public DataTemplate LastItemTemplate { get; set; }
public DataTemplate SingleItemTemplate { get; set; }

public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
ListView lv = VisualTreeHelperEx.FindParentOfType<ListView>(container);
if (lv != null)
{
if (lv.Items.Count == 1)
{
return SingleItemTemplate;
}

int i = lv.Items.IndexOf(item);
if (i == 0)
{
return FirstItemTemplate;
}
else if (i == lv.Items.Count - 1)
{
return LastItemTemplate;
}
}
return DefaultTemplate;
}
}

最佳答案

作为一种替代方法,我建议绑定(bind) AlternationCount您的ItemsControl到您的集合中的项目数(例如 Count 属性)。然后,这将分配给您的 ItemsControl 中的每个容器独一无二的AlternationIndex (0, 1, 2, ... Count-1)。浏览此处获取更多信息:

http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.alternationcount.aspx

一旦每个容器都有一个唯一的 AlternationIndex您可以使用 DataTrigger在您的容器中Style设置ItemTemplate基于指数。这可以使用 MultiBinding 来完成。使用返回 True 的转换器如果索引等于计数,False除此以外。当然,您也可以围绕这种方法构建一个选择器。除了转换器之外,这种方法都很好,因为它是仅 XAML 的解决方案。

使用 ListBox 的示例:

<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Collections="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:l="clr-namespace:WpfApplication4"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<Collections:ArrayList x:Key="MyCollection">
<System:String>Item One</System:String>
<System:String>Item Two</System:String>
<System:String>Item Three</System:String>
</Collections:ArrayList>

<l:MyAlternationEqualityConverter x:Key="MyAlternationEqualityConverter" />

<Style x:Key="MyListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource MyAlternationEqualityConverter}">
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ListBox}}" Path="Items.Count" />
<Binding RelativeSource="{RelativeSource Self}" Path="(ItemsControl.AlternationIndex)" />
</MultiBinding>
</DataTrigger.Binding>
<!-- Could set the ItemTemplate instead -->
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Resources>

<ListBox ItemsSource="{Binding Source={StaticResource MyCollection}}"
AlternationCount="{Binding RelativeSource={RelativeSource Self}, Path=Items.Count}"
ItemContainerStyle="{StaticResource MyListBoxItemStyle}" />
</Grid>

转换器可能看起来像这样的地方:
class MyAlternationEqualityConverter : IMultiValueConverter
{
#region Implementation of IMultiValueConverter

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values != null && values.Length == 2 &&
values[0] is int && values[1] is int)
{
return Equals((int) values[0], (int) values[1] + 1);
}

return DependencyProperty.UnsetValue;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}

#endregion
}

关于wpf - ListView 中第一个和最后一个项目的不同项目模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7834987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com