Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Carousel : ItemsControl
- {
- private ScrollViewer _scrollviewer;
- private ScrollViewer ScrollViewer => _scrollviewer ?? (_scrollviewer = (ScrollViewer)GetTemplateChild("PART_scroller"));
- protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
- {
- if (element is FrameworkElement fe)
- {
- var sv = ScrollViewer;
- fe.SetBinding(MaxWidthProperty, new Binding { Path = new PropertyPath(nameof(ScrollViewer.ViewportWidth)), Source = sv, Converter = new ItemsSizeConverter() });
- }
- base.PrepareContainerForItemOverride(element, item);
- }
- class ItemsSizeConverter : IValueConverter
- {
- object IValueConverter.Convert(object value, Type targetType, object parameter, string language)
- => value is double d ? d * 0.9 : DependencyProperty.UnsetValue;
- object IValueConverter.ConvertBack(object value, Type targetType, object parameter, string language) => throw new NotImplementedException();
- }
- }
- public class CarouselPanel : StackPanel, IScrollSnapPointsInfo
- {
- public const double OutsideGutter = 100;
- public CarouselPanel()
- {
- Orientation = Orientation.Horizontal;
- Padding = new Thickness(OutsideGutter, 0, OutsideGutter, 0);
- }
- IReadOnlyList<float> IScrollSnapPointsInfo.GetIrregularSnapPoints(Orientation orientation, SnapPointsAlignment alignment)
- {
- if (orientation == Orientation.Vertical)
- return Array.Empty<float>();
- return points().ToArray();
- IEnumerable<float> points()
- {
- yield return 0f;
- var x = OutsideGutter;// this.FindParent<ScrollViewer>().ViewportWidth * 0.1;
- foreach (var c in Children.OfType<FrameworkElement>())
- {
- yield return (float)(x + c.ActualWidth / 2);
- x += c.ActualWidth;
- }
- yield return (float)ActualWidth;
- }
- }
- }
Add Comment
Please, Sign In to add comment