<UserControl x:Class="CustomControl.UserControl.LbtCtrl"
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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:CustomControl.UserControl"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" x:Name="me" >
<UserControl.Resources>
<SolidColorBrush x:Key="myBrush" Color="Gray" Opacity="0.5" />
<local:IsVisibilityConverter x:Key="visibilityCvt"/>
<Style TargetType="RadioButton">
<Setter Property="IsChecked" Value="False"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="#555"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
<Setter Property="Margin" Value="10 10 0 30"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<Grid>
<Ellipse x:Name="ellispe" Fill="AliceBlue" Width="15" Height="15"
HorizontalAlignment="Center" VerticalAlignment="Center"
StrokeThickness="1" Stroke="{StaticResource myBrush}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Fill" TargetName="ellispe" Value="#FF1698E2"/>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Fill" TargetName="ellispe" Value="AliceBlue"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="imgKey" TargetType="{x:Type Image}">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Opacity" Value=".7"/>
<Setter Property="Visibility" Value="{Binding RelativeSource={RelativeSource
Mode=FindAncestor,AncestorType=Grid},Path=IsMouseOver,FallbackValue=Hidden,Converter=
{StaticResource visibilityCvt}}"/>
</Style>
</UserControl.Resources>
<Grid Width="600" Height="255">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Image Grid.ColumnSpan="3" Stretch="UniformToFill" x:Name="displayImg" />
<Image Grid.Column="0" VerticalAlignment="Center"
Width="41" Height="69" Cursor="Hand" Source="/CustomControl;component/Images/icon-left.png"
Margin="5 5 5 5" Style="{StaticResource imgKey}" MouseDown="PreImg_MouseDown"/>
<Image Grid.Column="2" VerticalAlignment="Center"
Width="41" Height="69" Cursor="Hand" Source="/CustomControl;component/Images/icon-right.png"
Margin="5 5 5 5" Style="{StaticResource imgKey}" MouseDown="NextImg_MouseDown"/>
<StackPanel x:Name="stack" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Center"/>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace CustomControl.UserControl
{
/// <summary>
/// LbtCtrl.xaml 的交互逻辑 轮播图控件
/// </summary>
public partial class LbtCtrl : System.Windows.Controls.UserControl
{
public LbtCtrl()
{
InitializeComponent();
}
private List<BitmapImage> currentListImg = null;
public List<BitmapImage> CurrentListimg
{
set
{
initLbtCtrl(value);
}
}
private List<RadioButton> currentListRdtBtn = null;
private Timer timer = null;
public void initLbtCtrl(List<BitmapImage> imgList)
{
if (imgList == null) { return; }
bool isChecked = true;
int rdIndex = 0;
imgList.ForEach((item)=>{
RadioButton rdBtn = new RadioButton();
if (isChecked) { rdBtn.IsChecked = true; isChecked = false; }
rdBtn.Tag = rdIndex;
rdIndex++;
rdBtn.Click += RdBtn_Click;
stack.Children.Add(rdBtn);
if (currentListImg == null) { currentListImg = new List<BitmapImage>(); }
if (currentListRdtBtn == null) { currentListRdtBtn = new List<RadioButton>(); }
currentListImg.Add(item);
currentListRdtBtn.Add(rdBtn);
});
this.displayImg.Source = currentListImg[0];
if (timer == null)
{
timer = new Timer(new TimerCallback((p)=>
{
this.Dispatcher.Invoke(new Action(() =>
{
NextImg_MouseDown(null, null);
}));
})
,null,3000,3000);
}
}
private void PreImg_MouseDown(object sender, MouseButtonEventArgs e)
{
int count = stack.Children.Count;
bool IsFirstEnter = true;
foreach (RadioButton item in stack.Children)
{
if (item.IsChecked==true&& IsFirstEnter)
{
IsFirstEnter = false;
item.IsChecked = false;
int index =(int) item.Tag;
if (index==0)
{
index = count-1;
}
else
{
index--;
}
this.displayImg.Source = currentListImg[index];
currentListRdtBtn[index].IsChecked = true;
}
}
}
private void NextImg_MouseDown(object sender, MouseButtonEventArgs e)
{
int count = stack.Children.Count;
bool IsFirstEnter = true;
foreach (RadioButton item in stack.Children)
{
if (item.IsChecked == true&&IsFirstEnter)
{
IsFirstEnter = false;
item.IsChecked = false;
int index = (int)item.Tag;
if (index == count-1)
{
index = 0;
}
else
{
index++;
}
this.displayImg.Source = currentListImg[index];
currentListRdtBtn[index].IsChecked = true;
}
}
}
private void RdBtn_Click(object sender, RoutedEventArgs e)
{
foreach (RadioButton item in stack.Children)
{
item.IsChecked = false;
}
RadioButton rdBtn = (RadioButton)sender;
rdBtn.IsChecked = true;
this.displayImg.Source = currentListImg[(int)rdBtn.Tag];
}
}
/// <summary>
/// 控件 显示/隐藏转换器
/// </summary>
public class IsVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null)
{
bool isOk = (bool)value;
if (isOk)
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed;
}
}
return true;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
}
public partial class TestLbtWin : Window
{
public TestLbtWin()
{
InitializeComponent();
List<BitmapImage> imgList = new List<BitmapImage>();
imgList.Add(new BitmapImage(new Uri(@"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201305%2F30%2F220025pxfkhykvkgkvuktq.jpg&refer=http%3A%2F%2Fattach.bbs.miui.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1613874045&t=192b795c62c4fb872a34b03aee7b50b3", UriKind.Absolute)));
imgList.Add(new BitmapImage(new Uri(@"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201304%2F25%2F195133e7a1l7b4f5117y4y.jpg&refer=http%3A%2F%2Fattach.bbs.miui.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1613874045&t=33dc201b111443c2ede5eac0fb52f048", UriKind.Absolute)));
imgList.Add(new BitmapImage(new Uri(@"https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=1652123795,1945063222&fm=26&gp=0.jpg", UriKind.Absolute)));
imgList.Add(new BitmapImage(new Uri(@"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fattachments.gfan.com%2Fforum%2Fattachments2%2F201301%2F29%2F125313339n39z82ydzc32y.jpg&refer=http%3A%2F%2Fattachments.gfan.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1613874045&t=4eb9547b83ae8c0af716a78e22f4268e", UriKind.Absolute)));
imgList.Add(new BitmapImage(new Uri(@"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201306%2F21%2F220728m5zcr5ecr7cqq7bw.jpg&refer=http%3A%2F%2Fattach.bbs.miui.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1613874045&t=ae3fd055772ecd3860128e58fc2f1344", UriKind.Absolute)));
lbtCtrl.CurrentListimg = imgList;
}
}