|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +namespace CommunityToolkit.Labs.WinUI; |
| 6 | +public partial class SegmentedMarginConverter : DependencyObject, IValueConverter |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Identifies the <see cref="LeftItemMargin"/> property. |
| 10 | + /// </summary> |
| 11 | + public static readonly DependencyProperty LeftItemMarginProperty = |
| 12 | + DependencyProperty.Register(nameof(LeftItemMargin), typeof(Thickness), typeof(SegmentedMarginConverter), new PropertyMetadata(null)); |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Gets or sets the margin of the first item |
| 16 | + /// </summary> |
| 17 | + public Thickness LeftItemMargin |
| 18 | + { |
| 19 | + get { return (Thickness)GetValue(LeftItemMarginProperty); } |
| 20 | + set { SetValue(LeftItemMarginProperty, value); } |
| 21 | + } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Identifies the <see cref="MiddleItemMargin"/> property. |
| 25 | + /// </summary> |
| 26 | + public static readonly DependencyProperty MiddleItemMarginProperty = |
| 27 | + DependencyProperty.Register(nameof(MiddleItemMargin), typeof(Thickness), typeof(SegmentedMarginConverter), new PropertyMetadata(null)); |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets or sets the margin of any middle item |
| 31 | + /// </summary> |
| 32 | + public Thickness MiddleItemMargin |
| 33 | + { |
| 34 | + get { return (Thickness)GetValue(MiddleItemMarginProperty); } |
| 35 | + set { SetValue(MiddleItemMarginProperty, value); } |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Identifies the <see cref="RightItemMargin"/> property. |
| 40 | + /// </summary> |
| 41 | + public static readonly DependencyProperty RightItemMarginProperty = |
| 42 | + DependencyProperty.Register(nameof(RightItemMargin), typeof(Thickness), typeof(SegmentedMarginConverter), new PropertyMetadata(null)); |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets or sets the margin of the last item |
| 46 | + /// </summary> |
| 47 | + public Thickness RightItemMargin |
| 48 | + { |
| 49 | + get { return (Thickness)GetValue(RightItemMarginProperty); } |
| 50 | + set { SetValue(RightItemMarginProperty, value); } |
| 51 | + } |
| 52 | + |
| 53 | + public object Convert(object value, Type targetType, object parameter, string language) |
| 54 | + { |
| 55 | + var segmentedItem = value as SegmentedItem; |
| 56 | + var listView = ItemsControl.ItemsControlFromItemContainer(segmentedItem); |
| 57 | + |
| 58 | + int index = listView.IndexFromContainer(segmentedItem); |
| 59 | + |
| 60 | + if (index == 0) |
| 61 | + { |
| 62 | + return LeftItemMargin; |
| 63 | + } |
| 64 | + else if (index == listView.Items.Count - 1) |
| 65 | + { |
| 66 | + return RightItemMargin; |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + return MiddleItemMargin; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public object ConvertBack(object value, Type targetType, object parameter, string language) |
| 75 | + { |
| 76 | + return value; |
| 77 | + } |
| 78 | +} |
0 commit comments