WPF自定义控件(教程含源码)-轮播控件、仿 layui Carousel 控件

控件需求

制作一个轮播控件。要求包含左右翻页按钮、索引指示、索引点击跳转。

控件效果

rcarousel

 控件基本元素

  • Button:控制左右滑动。

Button 由一个透明的 Ellispe 和 描绘箭头的 Path 公共组成,这里直接重写 Button 的 模板即可,xaml代码样式如下:

    <Style x:Key="carouselButton" TargetType="{x:Type Button}">
        <Setter Property="RenderTransformOrigin" Value=".5 .5" />
        <Setter Property="Width" Value="40" />
        <Setter Property="Height" Value="40" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Ellipse Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Fill="#33000000" x:Name="root" />
                        <Path Data="M25,10 L13,20 L25,30" StrokeThickness="2" Stroke="White" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="root" Property="Fill" Value="#5a000000" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
  • RWrapPanel:放置内容项目,实现平滑滚动。

参考上一篇文章:实现可以平滑滚动的Panel 控件

  • RCarouselIndicator:索引显示,索引跳转。

RCarouselIndicator 继承 ListView,新增 Count 属性,通过Count 属性控制圆点个数。使用时间 SelectedIndex 与 RCarousel 的 Index 进行双向绑定。

RCarouselIndicatorSelected 通过传入 SelectedIndex 和 Value(存放在Tag内)控制控件是否选中。

RCarouselIndicator的 xaml 代码 和 c# 代码如下:

    <local:RCarouselIndicatorSelected x:Key="RCarouselIndicatorSelected" />

    <Style x:Key="indexPoint" TargetType="Ellipse">
        <Setter Property="Fill" Value="#7dffffff" />
        <Setter Property="Width" Value="14" />
        <Setter Property="Height" Value="14" />
        <Setter Property="Margin" Value="3" />
    </Style>

<Style TargetType="local:RCarouselIndicator">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:RCarouselIndicator">
                    <Border SnapsToDevicePixels="True" Background="#33000000"  VerticalAlignment="Bottom" Margin="0 0 0 15" HorizontalAlignment="Center" MinWidth="14" CornerRadius="13">
                        <WrapPanel VerticalAlignment="Center" Margin="3" IsItemsHost="True" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="ListViewItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ListViewItem">
                                <ContentPresenter />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Grid>
                        <Ellipse Style="{StaticResource indexPoint}" Tag="{Binding}" Cursor="Hand">
                            <Ellipse.Fill>
                                <MultiBinding Converter="{StaticResource RCarouselIndicatorSelected}">
                                    <Binding Path="SelectedIndex" RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=local:RCarouselIndicator}" />
                                    <Binding Path="Tag" RelativeSource="{RelativeSource Mode=Self}" />
                                </MultiBinding>
                            </Ellipse.Fill>
                        </Ellipse>
                    </Grid>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    public partial class RCarouselIndicator: ListView {
        static RCarouselIndicator() {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(RCarouselIndicator), new FrameworkPropertyMetadata(typeof(RCarouselIndicator)));
        }

        public static readonly DependencyProperty CountProperty =
            DependencyProperty.Register("Count", typeof(int), typeof(RCarouselIndicator), new FrameworkPropertyMetadata(1, CountChanged));

        static void CountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            if (d is RCarouselIndicator control) {
                var count = Convert.ToInt32(e.NewValue);
                control.Items.Clear();
                Enumerable.Range(0, count).ToList().ForEach(t => control.Items.Add(t));
            }
        }

        public int Count {
            get => (int)GetValue(CountProperty);
            set => SetValue(CountProperty, value);
        }
    }

    public class RCarouselIndicatorSelected : IMultiValueConverter {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
            if (values.Length == 2 && values[0] is int v1 && values[1] is int v2 && v1 == v2) {
                return Brushes.White;
            } else {
                return new SolidColorBrush(Color.FromArgb(0x7d, 0xff, 0xff, 0xff));
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
            throw new NotImplementedException();
        }
    }

将基本元素组合

RCarousel 的 xaml代码

    <Style x:Key="RCarousel_Item" TargetType="ListBoxItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <ContentPresenter />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ControlTemplate x:Key="RCarousel_Template" TargetType="local:RCarousel">
        <Border SnapsToDevicePixels="True" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
            <Grid>
                <ScrollViewer  VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden" CanContentScroll="True">
                    <local:RWrapPanel IsItemsHost="True" x:Name="panel" />
                </ScrollViewer>

                <local:RCarouselIndicator Count="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Items.Count, Mode=OneWay}"
                                          SelectedIndex="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Index, Mode=TwoWay}" />

                <Button Style="{StaticResource carouselButton}" HorizontalAlignment="Left" Margin="15 0 0 0" Visibility="Collapsed" x:Name="btn1"/>
                <Button Style="{StaticResource carouselButton}" HorizontalAlignment="Right" Margin="0 0 15 0" Visibility="Collapsed" x:Name="btn2">
                    <Button.RenderTransform>
                        <RotateTransform Angle="180" />
                    </Button.RenderTransform>
                </Button>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="true" >
                <Setter TargetName="btn1" Property="Visibility" Value="Visible" />
                <Setter TargetName="btn2" Property="Visibility" Value="Visible" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style TargetType="local:RCarousel">
        <Setter Property="Template" Value="{StaticResource RCarousel_Template}" />
        <Setter Property="ItemContainerStyle" Value="{StaticResource RCarousel_Item}" />
    </Style>

RCarousel 的 c# 代码 

    public partial class RCarousel: ListBox {
        RWrapPanel panel;

        static RCarousel() {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(RCarousel), new FrameworkPropertyMetadata(typeof(RCarousel)));
        }

        public static readonly DependencyProperty IndexProperty =
            DependencyProperty.Register("Index", typeof(int), typeof(RCarousel), new FrameworkPropertyMetadata(0, IndexChanged));

        static void IndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            if (d is RCarousel control) {
                var index = Convert.ToInt32(e.NewValue);
                control.panel.SetHorizontalOffset(index * control.ActualWidth);
            }
        }

        public int Index {
            get => (int)GetValue(IndexProperty);
            set => SetValue(IndexProperty, value);
        }

        public override void OnApplyTemplate() {
            base.OnApplyTemplate();

            panel = GetTemplateChild("panel") as RWrapPanel;

            Button btn1 = GetTemplateChild("btn1") as Button;
            Button btn2 = GetTemplateChild("btn2") as Button;

            btn1.Click += Btn1_Click;
            btn2.Click += Btn2_Click;

            var count = this.Items.Count;
        }

        private void Btn1_Click(object sender, RoutedEventArgs e) {
            panel.PageLeft();
            if (Index > 0) Index--;
        }

        private void Btn2_Click(object sender, RoutedEventArgs e) {
            panel.PageRight();
            if (Index < Items.Count - 1) Index++;
        }
    }

在xaml 内调用该控件

<UserControl x:Class="WPFCustomControl.Pages.RCarouselDemo"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WPFCustomControl.Pages"
             xmlns:cr="clr-namespace:WPFCustomControl.Controls"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <cr:RCarousel x:Name="root" >
            <Border Background="#009688" Width="{Binding ElementName=root, Path=ActualWidth}"  Height="{Binding ElementName=root, Path=ActualHeight}">
                <TextBlock Text="项目1" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" />
            </Border>
            <Border Background="#5FB878" Width="{Binding ElementName=root, Path=ActualWidth}"  Height="{Binding ElementName=root, Path=ActualHeight}">
                <TextBlock Text="项目2" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" />
            </Border>
            <Border Background="#009688" Width="{Binding ElementName=root, Path=ActualWidth}"  Height="{Binding ElementName=root, Path=ActualHeight}">
                <TextBlock Text="项目3" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" />
            </Border>
            <Border Background="#5FB878" Width="{Binding ElementName=root, Path=ActualWidth}"  Height="{Binding ElementName=root, Path=ActualHeight}">
                <TextBlock Text="项目4" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" />
            </Border>
        </cr:RCarousel>
    </Grid>
</UserControl>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lhyriver

制作不易,打赏给我提供动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值