42 lines
1.6 KiB
C#
42 lines
1.6 KiB
C#
using System.Globalization;
|
|
using System.Windows.Data;
|
|
|
|
namespace Melskin.Converters.Internal;
|
|
|
|
/// <summary>
|
|
/// 用于判断集合中的项目是否为最后一个项目的转换器。
|
|
/// 此转换器通常在数据绑定场景中使用,以识别并处理列表或集合的最后一个元素。
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 使用此转换器时,需要传递一个包含当前项和整个集合的参数。转换器将基于这些信息来确定当前项是否是集合中的最后一项。
|
|
/// </remarks>
|
|
internal class IsLastItemConverter : IMultiValueConverter
|
|
{
|
|
/// <summary>
|
|
/// IsLastItemConverter 的单例实例,用于在 XAML 中方便地引用和使用该转换器。
|
|
/// 通过此静态只读属性,可以在多个地方重复使用同一个 IsLastItemConverter 实例,
|
|
/// 而无需每次创建新的实例,从而提高性能并简化资源管理。
|
|
/// </summary>
|
|
public static readonly IsLastItemConverter Instance = new();
|
|
/// <inheritdoc/>
|
|
public object Convert(object[]? values, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (values == null || values.Length < 2 || values[0] == null || values[1] == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (values[0] is ItemsControl itemsControl && values[1] is Control item)
|
|
{
|
|
return itemsControl.ItemContainerGenerator.IndexFromContainer(item) == itemsControl.Items.Count - 1;
|
|
}
|
|
|
|
return Binding.DoNothing;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
|
{
|
|
return [value];
|
|
}
|
|
} |