<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.Resources>
<controls:ListContainerIndexConverter x:Key="ListContainerIndexConverter" />
</ItemsControl.Resources>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="controls:ListContainerHelper.Index" Value="{Binding Path=AlternationCount, Converter={StaticResource ListContainerIndexConverter}, Mode=OneTime, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}" />
<Setter Property="controls:ListContainerHelper.Count" Value="{Binding Path=Items.Count, Mode=OneTime, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type vm:PageRealtimePanelItem}">
<Border Margin="0,0,16,0"
BorderBrush="{DynamicResource GridPowerPulseBorderColorBrush}"
Visibility="{Binding IsVisiable, Converter={StaticResource BoolToVisibleConverter}}">
<Border.BorderThickness>
<MultiBinding Converter="{x:Static controls:ListContainerBorderThicknessConverter.Default}" ConverterParameter="1">
<Binding Path="(controls:ListContainerHelper.Index)" RelativeSource="{RelativeSource AncestorType=ContentPresenter}" />
<Binding Path="(controls:ListContainerHelper.Count)" RelativeSource="{RelativeSource AncestorType=ContentPresenter}" />
</MultiBinding>
</Border.BorderThickness>
<Border.CornerRadius>
<MultiBinding Converter="{x:Static controls:ListContainerCornerRadiusConverter.Default}" ConverterParameter="8">
<Binding Path="(controls:ListContainerHelper.Index)" RelativeSource="{RelativeSource AncestorType=ContentPresenter}" />
<Binding Path="(controls:ListContainerHelper.Count)" RelativeSource="{RelativeSource AncestorType=ContentPresenter}" />
</MultiBinding>
</Border.CornerRadius>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Grid.Column="0"
BorderThickness="0,0,1,0">
<TextBlock Margin="3,0,0,0"
VerticalAlignment="Center"
Text="{Binding (controls:ListContainerHelper.Index)}" />
</Border>
<Border Grid.Column="1">
<TextBlock Margin="0,0,3,0"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="{Binding (controls:ListContainerHelper.Count)}" />
</Border>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
public static class ListContainerHelper
{
public static readonly DependencyProperty IndexProperty =
DependencyProperty.RegisterAttached("Index", typeof(int), typeof(ListContainerHelper), new PropertyMetadata(0));
public static int GetIndex(DependencyObject obj)
{
return (int)obj.GetValue(IndexProperty);
}
public static void SetIndex(DependencyObject obj, int value)
{
obj.SetValue(IndexProperty, value);
}
public static readonly DependencyProperty CountProperty =
DependencyProperty.RegisterAttached("Count", typeof(int), typeof(ListContainerHelper), new PropertyMetadata(0));
public static int GetCount(DependencyObject obj)
{
return (int)obj.GetValue(CountProperty);
}
public static void SetCount(DependencyObject obj, int value)
{
obj.SetValue(CountProperty, value);
}
}
[ValueConversion(typeof(object), typeof(int))]
public sealed class ListContainerIndexConverter : IListContainerIndexConverter
{
public interface IListContainerIndexConverter : IValueConverter
{
public int Index { get; set; }
}
public int Index { get; set; } = 0;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ++Index;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
[ValueConversion(typeof(int), typeof(Thickness))]
public sealed class ListContainerBorderThicknessConverter : IMultiValueConverter
{
public static readonly ListContainerBorderThicknessConverter Default = new();
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
Thickness thickness = default;
if (double.TryParse(parameter?.ToString(), out double v))
{
///
}
if (values[0] is int index)
{
if (index == 1)
{
thickness = new Thickness(v, v, v, v);
}
else
{
thickness = new Thickness(v, 0, v, v);
}
}
return thickness;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
[ValueConversion(typeof(int), typeof(CornerRadius))]
public sealed class ListContainerCornerRadiusConverter : IMultiValueConverter
{
public static readonly ListContainerCornerRadiusConverter Default = new();
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
CornerRadius radius = default;
if (double.TryParse(parameter?.ToString(), out double v))
{
///
}
if (values[0] is int index && values[1] is int count)
{
if (index == 1)
{
radius = new CornerRadius(v, v, 0, 0);
}
else
{
radius = new CornerRadius(0, 0, 0, 0);
}
if (index == count)
{
radius = new CornerRadius(radius.TopLeft, radius.TopRight, v, v);
}
}
return radius;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}