添加轮播控件

This commit is contained in:
NaBian committed 2018-08-23 18:07:42 +08:00
1 parent 1506d23ce4
commit 2e3c13ed4d
15 files changed
+460 -7

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
<UserControl x:Class="HandyControl.Controls.Carousel"
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:interactivity="clr-namespace:HandyControl.Interactivity"
mc:Ignorable="d"
BorderThickness="1"
Background="Transparent"
Name="UserControlMain"
BorderBrush="{StaticResource BorderBrush}"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="GridTop" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:.1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="FrameworkElement.MouseLeave">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="GridTop" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:.1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</UserControl.Triggers>
<Grid ClipToBounds="True">
<ListBox HorizontalAlignment="Left" Name="ListBoxMain" BorderThickness="0" ItemsSource="{Binding ItemsSource,ElementName=UserControlMain}" Style="{StaticResource StackPanelListBox}"></ListBox>
<Grid Name="GridTop" Opacity="0">
<Button Click="ButtonPrev_OnClick" Name="ButtonPrev" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="30,0,0,0" Style="{StaticResource ButtonOpacityStyle}">
<Border Width="50" Height="100" Background="#44000000" CornerRadius="2">
<Path Margin="16" Fill="White" Style="{StaticResource LeftPathStyle}"></Path>
</Border>
</Button>
<Button Click="ButtonNext_OnClick" Name="ButtonNext" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,30,0" Style="{StaticResource ButtonOpacityStyle}">
<Border Width="50" Height="100" Background="#44000000" CornerRadius="2">
<Path Margin="16" Fill="White" Style="{StaticResource RightPathStyle}"></Path>
</Border>
</Button>
</Grid>
<StackPanel Button.Click="ButtonPages_OnClick" Name="PanelPage" Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center" Margin="0,0,0,20"></StackPanel>
</Grid>
</UserControl>
+258
View File
@@ -0,0 +1,258 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using HandyControl.Tools;
namespace HandyControl.Controls
{
/// <summary>
/// Carousel.xaml 的交互逻辑
/// </summary>
[DefaultProperty("Items")]
[ContentProperty("Items")]
public partial class Carousel
{
public static readonly DependencyProperty AutoRunProperty = DependencyProperty.Register(
"AutoRun", typeof(bool), typeof(Carousel), new PropertyMetadata(default(bool), (o, args) =>
{
var ctl = (Carousel)o;
ctl.TimerSwitch((bool)args.NewValue);
}));
public static readonly DependencyProperty IntervalProperty = DependencyProperty.Register(
"Interval", typeof(TimeSpan), typeof(Carousel), new PropertyMetadata(TimeSpan.FromSeconds(2)));
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
"ItemsSource", typeof(IEnumerable), typeof(Carousel), new PropertyMetadata(default(IEnumerable),
(o, args) =>
{
var ctl = (Carousel)o;
ctl.UpdatePageButtons();
}));
public static readonly DependencyProperty ExtendWidthProperty = DependencyProperty.Register(
"ExtendWidth", typeof(double), typeof(Carousel), new PropertyMetadata(default(double)));
public double ExtendWidth
{
get => (double)GetValue(ExtendWidthProperty);
set => SetValue(ExtendWidthProperty, value);
}
public static readonly DependencyProperty IsCenterProperty = DependencyProperty.Register(
"IsCenter", typeof(bool), typeof(Carousel), new PropertyMetadata(default(bool)));
public bool IsCenter
{
get => (bool)GetValue(IsCenterProperty);
set => SetValue(IsCenterProperty, value);
}
private int _pageIndex = -1;
private Button _selectedButton;
private DispatcherTimer _updateTimer;
private readonly List<double> _widthList = new List<double>();
public Carousel()
{
InitializeComponent();
Loaded += (s, e) => UpdatePageButtons();
}
/// <summary>
/// 是否自动跳转
/// </summary>
public bool AutoRun
{
get => (bool)GetValue(AutoRunProperty);
set => SetValue(AutoRunProperty, value);
}
/// <summary>
/// 跳转时间间隔
/// </summary>
public TimeSpan Interval
{
get => (TimeSpan)GetValue(IntervalProperty);
set => SetValue(IntervalProperty, value);
}
/// <summary>
/// 页码
/// </summary>
public int PageIndex
{
get => _pageIndex;
set
{
if (Items.Count == 0) return;
if (_pageIndex == value) return;
if (value < 0)
_pageIndex = Items.Count - 1;
else if (value >= Items.Count)
_pageIndex = 0;
else
_pageIndex = value;
UpdatePageButtons(_pageIndex);
}
}
public IEnumerable ItemsSource
{
get => (IEnumerable)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public ItemCollection Items => ListBoxMain.Items;
/// <summary>
/// 计时器开关
/// </summary>
private void TimerSwitch(bool run)
{
if (run)
{
_updateTimer = new DispatcherTimer
{
Interval = Interval
};
_updateTimer.Tick += UpdateTimer_Tick;
_updateTimer.Start();
}
else
{
_updateTimer.Tick -= UpdateTimer_Tick;
_updateTimer.Stop();
_updateTimer = null;
}
}
private void UpdateTimer_Tick(object sender, EventArgs e)
{
if (IsMouseOver) return;
PageIndex++;
}
/// <summary>
/// 更新页按钮
/// </summary>
public void UpdatePageButtons(int? index = null)
{
if (index == null)
{
var count = Items.Count;
_widthList.Clear();
_widthList.Add(0);
var width = .0;
foreach (FrameworkElement item in ListBoxMain.Items)
{
width += item.Width + item.Margin.Left + item.Margin.Right;
_widthList.Add(width);
}
ListBoxMain.Width = _widthList.Last() + ExtendWidth;
PanelPage.Children.Clear();
for (var i = 0; i < count; i++)
PanelPage.Children.Add(CreatePateButton());
if (count > 0)
{
var button = PanelPage.Children[0];
button.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent, button));
}
}
else
{
if (index >= 0 && index < PanelPage.Children.Count)
{
var button = PanelPage.Children[(int)index];
button.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent, button));
UpdateItemsPosition();
}
}
}
/// <summary>
/// 更新项的位置
/// </summary>
private void UpdateItemsPosition()
{
if (Items.Count == 0 || PageIndex == -1) return;
if (!IsCenter)
{
ListBoxMain.BeginAnimation(MarginProperty,
AnimationHelper.CreateAnimation(new Thickness(-_widthList[PageIndex], 0, 0, 0)));
}
else
{
var ctl = (FrameworkElement)Items[PageIndex];
var ctlWidth = ctl.Width + ctl.Margin.Left + ctl.Margin.Right;
ListBoxMain.BeginAnimation(MarginProperty,
AnimationHelper.CreateAnimation(
new Thickness(-_widthList[PageIndex] + (ActualWidth - ctlWidth) / 2, 0, 0, 0)));
}
}
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
UpdateItemsPosition();
}
/// <summary>
/// 创建页按钮
/// </summary>
/// <returns></returns>
private Button CreatePateButton()
{
var button = new Button
{
Style = TryFindResource("ButtonOpacityStyle") as Style,
Content = new Border
{
Width = 10,
Height = 10,
CornerRadius = new CornerRadius(5),
Background = Brushes.White,
Margin = new Thickness(5, 0, 5, 0),
BorderThickness = new Thickness(1),
BorderBrush = TryFindResource("PrimaryBrush") as Brush
}
};
return button;
}
private void ButtonPages_OnClick(object sender, RoutedEventArgs e)
{
if (_selectedButton != null && _selectedButton.Content is Border borderOri)
borderOri.Background = Brushes.White;
_selectedButton = e.OriginalSource as Button;
if (_selectedButton != null && _selectedButton.Content is Border border)
border.Background = TryFindResource("PrimaryBrush") as Brush;
var index = PanelPage.Children.IndexOf(_selectedButton);
if (index != -1)
PageIndex = index;
}
private void ButtonPrev_OnClick(object sender, RoutedEventArgs e)
{
PageIndex--;
}
private void ButtonNext_OnClick(object sender, RoutedEventArgs e)
{
PageIndex++;
}
}
}
+15
View File
@@ -31,6 +31,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="Controls\Carousel.xaml.cs">
<DependentUpon>Carousel.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\ColorPicker.xaml.cs">
<DependentUpon>ColorPicker.xaml</DependentUpon>
</Compile>
@@ -102,6 +105,10 @@
<Resource Include="Resources\colorPickerBack.png" />
</ItemGroup>
<ItemGroup>
<Page Include="Controls\Carousel.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Controls\ColorPicker.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -162,6 +169,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Themes\Styles\Base\ListBoxBaseStyle.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Themes\Styles\Base\LoadingBaseStyle.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@@ -186,6 +197,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Themes\Styles\ListBox.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Themes\Styles\Loading.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -0,0 +1,71 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!--默认ListBoxItem样式-->
<Style x:Key="ListBoxItemBaseStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Padding" Value="10,0"></Setter>
<Setter Property="Height" Value="32"></Setter>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="{DynamicResource DarkDefaultBrush}"/>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" Value="{DynamicResource PrimaryBrush}"/>
<Setter Property="Foreground" Value="{DynamicResource TextIconBrush}"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="Selector.IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListBoxBaseStyle" TargetType="{x:Type ListBox}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="ItemContainerStyle" Value="{StaticResource ListBoxItemBaseStyle}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
<ScrollViewer Focusable="false">
<ItemsPresenter Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
+53
View File
@@ -0,0 +1,53 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Base/ListBoxBaseStyle.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
<!--默认样式-->
<Style BasedOn="{StaticResource ListBoxBaseStyle}" TargetType="{x:Type ListBox}"></Style>
<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ListBoxItemFullStyle" BasedOn="{StaticResource ListBoxItemStyle}" TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="Height" Value="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"></Setter>
</Style>
<ItemsPanelTemplate x:Key="WrapPanelItemsPanelTemplate">
<WrapPanel FocusVisualStyle="{x:Null}"></WrapPanel>
</ItemsPanelTemplate>
<Style x:Key="WrapPanelListBox" BasedOn="{StaticResource ListBoxBaseStyle}" TargetType="{x:Type ListBox}">
<Setter Property="ItemsPanel" Value="{StaticResource WrapPanelItemsPanelTemplate}"/>
<Setter Property="ItemContainerStyle" Value="{StaticResource ListBoxItemStyle}"/>
</Style>
<ItemsPanelTemplate x:Key="StackPanelItemsPanelTemplate">
<StackPanel FocusVisualStyle="{x:Null}" Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
<Style x:Key="StackPanelListBox" BasedOn="{StaticResource ListBoxBaseStyle}" TargetType="{x:Type ListBox}">
<Setter Property="ItemsPanel" Value="{StaticResource StackPanelItemsPanelTemplate}"/>
<Setter Property="ItemContainerStyle" Value="{StaticResource ListBoxItemFullStyle}"/>
</Style>
</ResourceDictionary>
+1 -1
View File
@@ -20,11 +20,11 @@
<ResourceDictionary Source="GroupBox.xaml"></ResourceDictionary>
<ResourceDictionary Source="Pagination.xaml"></ResourceDictionary>
<ResourceDictionary Source="TabControl.xaml"></ResourceDictionary>
<ResourceDictionary Source="ListBox.xaml"></ResourceDictionary>
<ResourceDictionary Source="ComboBox.xaml"></ResourceDictionary>
<ResourceDictionary Source="ContextMenu.xaml"></ResourceDictionary>
<ResourceDictionary Source="AnimationPath.xaml"></ResourceDictionary>
<ResourceDictionary Source="ToolTip.xaml"></ResourceDictionary>-->
<ResourceDictionary Source="ListBox.xaml"></ResourceDictionary>
<ResourceDictionary Source="Loading.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
+7
View File
@@ -73,5 +73,12 @@
<Name>HandyControl</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Resource Include="Resources\1.jpg" />
<Resource Include="Resources\2.jpg" />
<Resource Include="Resources\3.jpg" />
<Resource Include="Resources\4.jpg" />
<Resource Include="Resources\5.jpg" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
+2 -2
View File
@@ -376,7 +376,7 @@
</Grid>
</Border>
<!-- 轮播-->
<!--<Border Visibility="Collapsed" Margin="0,10,0,0" Style="{StaticResource BorderRegionStyle}">
<Border Visibility="Collapsed" Margin="0,10,0,0" Style="{StaticResource BorderRegionStyle}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Header"/>
@@ -397,7 +397,7 @@
<Image Width="600" Stretch="UniformToFill" Source="Resources/5.jpg"></Image>
</controls:Carousel>
</Grid>
</Border>-->
</Border>
<!--时间条-->
<!--<Border Visibility="Collapsed" Margin="0,10,0,0" Style="{StaticResource BorderRegionStyle}">
<Grid>
Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

+8 -4
View File
@@ -2,10 +2,14 @@
让我们开门见山,HandyControl已经开源的控件截图有:
### 1、ColorPicker
### 1、颜色拾取器ColorPicker
![ColorPicker](https://raw.githubusercontent.com/NaBian/HandyControl/master/Resources/ColorPicker.gif)
![颜色拾取器ColorPicker](https://raw.githubusercontent.com/NaBian/HandyControl/master/Resources/ColorPicker.gif)
### 2、Loading
### 2、加载条Loading
![Loading](https://raw.githubusercontent.com/NaBian/HandyControl/master/Resources/Loading.gif)
![加载条Loading](https://raw.githubusercontent.com/NaBian/HandyControl/master/Resources/Loading.gif)
### 2、轮播Carousel
![轮播Carousel](https://raw.githubusercontent.com/NaBian/HandyControl/master/Resources/Loading.gif)
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB