How to correct the border clip convert not affect the border line ?
MainWindow.xaml
BorderClipConverter.cs
Download Project
![]()
MainWindow.xaml
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:wpf_ItemsControl_Wrapped" xmlns:sys="clr-namespace:System;assembly=mscorlib" x:Class="wpf_ItemsControl_Wrapped.MainWindow" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"><Window.Resources><local:BorderClipConverter x:Key="BClipConverter"/></Window.Resources><Grid><Grid.Resources><Style TargetType="{x:Type Button}"><Setter Property="Margin" Value="4"/></Style></Grid.Resources><Grid.RowDefinitions><RowDefinition Height="Auto" /><RowDefinition Height="Auto" /><RowDefinition Height="Auto" /><RowDefinition Height="*"/></Grid.RowDefinitions><StackPanel Orientation="Horizontal" Grid.Row="1" Margin="4, 8, 4, 8"><TextBlock Text="Current Value:" VerticalAlignment="Center" /><!--TBC--><!--<TextBlock Text="x" VerticalAlignment="Center" />--></StackPanel><StackPanel Orientation="Horizontal" Grid.Row="2" Margin="4, 8, 4, 8"><TextBlock Text="Time Elapsed: " VerticalAlignment="Center" /><!--TBC--><!--<TextBlock Text="x" VerticalAlignment="Center" />--></StackPanel><ListBox x:Name="lb" Grid.Row="3" Grid.IsSharedSizeScope="True" AlternationCount="10000" ItemsSource="{Binding Models}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"><ListBox.ItemContainerStyle><Style TargetType="{x:Type ListBoxItem}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type ListBoxItem}"><ContentPresenter/></ControlTemplate></Setter.Value></Setter></Style></ListBox.ItemContainerStyle><ListBox.ItemsPanel><ItemsPanelTemplate><WrapPanel Orientation="Horizontal" Margin="15, 0, 15, 0" /></ItemsPanelTemplate></ListBox.ItemsPanel><ListBox.ItemTemplate><DataTemplate><Grid><Grid.RowDefinitions><RowDefinition Height="Auto" /><RowDefinition Height="50" /></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition MinWidth="150" SharedSizeGroup="AllSameWidth" /></Grid.ColumnDefinitions><Grid><Border BorderThickness="1" BorderBrush="Gray" Height="50"><Border.Clip><MultiBinding Converter="{StaticResource BClipConverter}"><Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/><Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/><Binding Path="CornerRadius" RelativeSource="{RelativeSource Self}"/></MultiBinding></Border.Clip><Border.Style><Style TargetType="{x:Type Border}"><Style.Triggers><DataTrigger Binding="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="0"><Setter Property="CornerRadius" Value="10, 0, 0, 10" /></DataTrigger><DataTrigger Binding="{Binding IsLastItem, ElementName=lastInLine}" Value="true" ><Setter Property="CornerRadius" Value="0, 10, 10, 0" /></DataTrigger><DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" Value="1"><Setter Property="CornerRadius" Value="10, 10, 10, 10" /></DataTrigger></Style.Triggers></Style></Border.Style><Grid><local:ItemTracer Width="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}}" Container="{Binding ., RelativeSource={RelativeSource AncestorType={x:Type WrapPanel}}}" ContainerWidth="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type WrapPanel}}}" ItemIndex="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" ItemsCount="{Binding Items.Count, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" x:Name="lastInLine" /><TextBlock Text="{Binding CurrentValue}" VerticalAlignment="Center" HorizontalAlignment="Center" /></Grid></Border></Grid><TextBlock Text="{Binding StartTime, StringFormat=\{0:mm\\:ss\}}" Grid.Row="1" Margin="-14, 0, 0, 0" /><TextBlock Text="{Binding EndTime, StringFormat=\{0:mm\\:ss\}}" Grid.Row="1" HorizontalAlignment="Right"><TextBlock.RenderTransform><TranslateTransform X="14" Y="0" /></TextBlock.RenderTransform><TextBlock.Style><Style TargetType="{x:Type TextBlock}"><Setter Property="Visibility" Value="Visible"/><Style.Triggers><MultiDataTrigger><MultiDataTrigger.Conditions><Condition Binding="{Binding IsLastInLine, ElementName=lastInLine}" Value="False" /><Condition Binding="{Binding IsLastItem, ElementName=lastInLine}" Value="False" /></MultiDataTrigger.Conditions><Setter Property="Visibility" Value="Collapsed" /></MultiDataTrigger></Style.Triggers></Style></TextBlock.Style></TextBlock></Grid></DataTemplate></ListBox.ItemTemplate></ListBox></Grid></Window>
BorderClipConverter.cs
using System; using System.Windows; using System.Windows.Data; using System.Windows.Media; namespace wpf_ItemsControl_Wrapped { public class BorderClipConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (values.Length == 3 && values[0] is double && values[1] is double && values[2] is CornerRadius) { double width = (double)values[0]; double height = (double)values[1]; if (width < double.Epsilon || height < double.Epsilon) { return Geometry.Empty; } CornerRadius radius = (CornerRadius)values[2]; double max = Math.Max(radius.TopLeft, Math.Max(radius.TopRight, Math.Max(radius.BottomLeft, radius.BottomRight))); Geometry result = new RectangleGeometry(new Rect(0, 0, width, height), max, max); double halfWidth = width / 2; double halfHeight = height / 2; //topleft if (radius.TopLeft == 0) { result = new CombinedGeometry (GeometryCombineMode.Union, result, new RectangleGeometry (new Rect(0, 0, halfWidth, halfHeight), 0, 0)); } //topright if (radius.TopRight == 0) { result = new CombinedGeometry (GeometryCombineMode.Union, result, new RectangleGeometry (new Rect(halfWidth, 0, halfWidth, halfHeight), 0, 0)); } //bottomleft if (radius.BottomLeft == 0) { result = new CombinedGeometry (GeometryCombineMode.Union, result, new RectangleGeometry (new Rect(0, halfHeight, halfWidth, halfHeight), 50, 50)); } //bottomright if (radius.BottomRight == 0.0) { result = new CombinedGeometry (GeometryCombineMode.Union, result, new RectangleGeometry (new Rect(halfWidth, halfHeight, halfWidth, halfHeight), 0, 0)); } result.Freeze(); return result; } return DependencyProperty.UnsetValue; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }
Download Project