完成CustomPager

croire 3 years ago
parent 4b1af53db7
commit b31c7830d4

@ -1,9 +1,10 @@
<UserControl
x:Class="DownKyi.ui.CustomControl.CustomPager"
x:Class="DownKyi.CustomControl.CustomPager"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DownKyi.ui.CustomControl"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:DownKyi.CustomControl"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<UserControl.Resources>
@ -49,7 +50,7 @@
</DrawingImage.Drawing>
</DrawingImage>
<Style x:Key="pagerBorder" TargetType="Border">
<Style x:Key="pagerBorder" TargetType="{x:Type Border}">
<Setter Property="Background" Value="#FFFFFFFF" />
<Setter Property="BorderBrush" Value="#FFD7DDE4" />
<Setter Property="BorderThickness" Value="1" />
@ -66,14 +67,48 @@
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="pagerButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#FFFFFFFF" />
<Setter Property="BorderBrush" Value="#FFD7DDE4" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Width" Value="30" />
<Setter Property="Height" Value="30" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="4">
<ContentPresenter
Name="content"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="BorderBrush" Value="#FF00A1D6" />
</Trigger>
<Trigger Property="IsMouseOver" Value="false">
<Setter Property="BorderBrush" Value="#FFD7DDE4" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<StackPanel Orientation="Horizontal">
<Border
Name="namePrevious"
<StackPanel Orientation="Horizontal" Visibility="{Binding Visibility}">
<Button
x:Name="namePrevious"
Margin="0,0,8,0"
MouseLeftButtonUp="Previous_MouseLeftButtonUp"
Style="{StaticResource pagerBorder}">
Command="{Binding PreviousCommand}"
Style="{StaticResource pagerButton}"
Visibility="{Binding PreviousVisibility}">
<Image
Width="16"
Height="16"
@ -83,27 +118,28 @@
<Style TargetType="Image">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=namePrevious}" Value="True">
<Setter Property="Source" Value="{DynamicResource ToLeftHoverDrawingImage}" />
<Setter Property="Source" Value="{StaticResource ToLeftHoverDrawingImage}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=namePrevious}" Value="False">
<Setter Property="Source" Value="{DynamicResource ToLeftDrawingImage}" />
<Setter Property="Source" Value="{StaticResource ToLeftDrawingImage}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Border>
</Button>
<Border
Name="nameFirst"
<Button
x:Name="nameFirst"
Margin="0,0,4,0"
MouseLeftButtonUp="First_MouseLeftButtonUp"
Style="{StaticResource pagerBorder}">
Command="{Binding FirstCommand}"
Style="{StaticResource pagerButton}"
Visibility="{Binding FirstVisibility}">
<TextBlock
Name="nameFirstText"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="14">
FontSize="14"
Text="{Binding First, Mode=TwoWay}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
@ -117,13 +153,13 @@
</Style>
</TextBlock.Style>
</TextBlock>
</Border>
</Button>
<Border
Name="nameLeftJump"
Width="20"
Height="40"
Margin="0,0,4,0">
Margin="0,0,4,0"
Visibility="{Binding LeftJumpVisibility}">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
@ -131,16 +167,17 @@
Text="&#x2022;&#x2022;&#x2022;" />
</Border>
<Border
Name="namePreviousSecond"
<Button
x:Name="namePreviousSecond"
Margin="0,0,4,0"
MouseLeftButtonUp="PreviousSecond_MouseLeftButtonUp"
Style="{StaticResource pagerBorder}">
Command="{Binding PreviousSecondCommand}"
Style="{StaticResource pagerButton}"
Visibility="{Binding PreviousSecondVisibility}">
<TextBlock
Name="namePreviousSecondText"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="14">
FontSize="14"
Text="{Binding PreviousSecond, Mode=TwoWay}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
@ -154,18 +191,19 @@
</Style>
</TextBlock.Style>
</TextBlock>
</Border>
</Button>
<Border
Name="namePreviousFirst"
<Button
x:Name="namePreviousFirst"
Margin="0,0,4,0"
MouseLeftButtonUp="PreviousFirst_MouseLeftButtonUp"
Style="{StaticResource pagerBorder}">
Command="{Binding PreviousFirstCommand}"
Style="{StaticResource pagerButton}"
Visibility="{Binding PreviousFirstVisibility}">
<TextBlock
Name="namePreviousFirstText"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="14">
FontSize="14"
Text="{Binding PreviousFirst, Mode=TwoWay}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
@ -179,46 +217,32 @@
</Style>
</TextBlock.Style>
</TextBlock>
</Border>
</Button>
<Border
Name="nameCurrent"
Margin="0,0,4,0"
Background="#FF00A1D6"
BorderThickness="0"
MouseLeftButtonUp="Current_MouseLeftButtonUp"
Style="{StaticResource pagerBorder}">
<TextBlock
Name="nameCurrentText"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="14"
Foreground="#FFFFFFFF">
<!--<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=nameCurrent}" Value="True">
<Setter Property="Foreground" Value="#FF00A1D6"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=nameCurrent}" Value="False">
<Setter Property="Foreground" Value="#FF657180"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>-->
</TextBlock>
Foreground="#FFFFFFFF"
Text="{Binding Current, Mode=TwoWay}" />
</Border>
<Border
Name="nameNextFirst"
<Button
x:Name="nameNextFirst"
Margin="0,0,4,0"
MouseLeftButtonUp="NextFirst_MouseLeftButtonUp"
Style="{StaticResource pagerBorder}">
Command="{Binding NextFirstCommand}"
Style="{StaticResource pagerButton}"
Visibility="{Binding NextFirstVisibility}">
<TextBlock
Name="nameNextFirstText"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="14">
FontSize="14"
Text="{Binding NextFirst, Mode=TwoWay}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
@ -232,18 +256,19 @@
</Style>
</TextBlock.Style>
</TextBlock>
</Border>
</Button>
<Border
Name="nameNextSecond"
<Button
x:Name="nameNextSecond"
Margin="0,0,4,0"
MouseLeftButtonUp="NextSecond_MouseLeftButtonUp"
Style="{StaticResource pagerBorder}">
Command="{Binding NextSecondCommand}"
Style="{StaticResource pagerButton}"
Visibility="{Binding NextSecondVisibility}">
<TextBlock
Name="nameNextSecondText"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="14">
FontSize="14"
Text="{Binding NextSecond, Mode=TwoWay}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
@ -257,13 +282,13 @@
</Style>
</TextBlock.Style>
</TextBlock>
</Border>
</Button>
<Border
Name="nameRightJump"
Width="20"
Height="40"
Margin="0,0,4,0">
Margin="0,0,4,0"
Visibility="{Binding RightJumpVisibility}">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
@ -271,16 +296,17 @@
Text="&#x2022;&#x2022;&#x2022;" />
</Border>
<Border
Name="nameLast"
<Button
x:Name="nameLast"
Margin="0,0,4,0"
MouseLeftButtonUp="Last_MouseLeftButtonUp"
Style="{StaticResource pagerBorder}">
Command="{Binding LastCommand}"
Style="{StaticResource pagerButton}"
Visibility="{Binding LastVisibility}">
<TextBlock
Name="nameLastText"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="14">
FontSize="14"
Text="{Binding Count}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
@ -294,13 +320,14 @@
</Style>
</TextBlock.Style>
</TextBlock>
</Border>
</Button>
<Border
Name="nameNext"
<Button
x:Name="nameNext"
Margin="4,0,4,0"
MouseLeftButtonUp="Next_MouseLeftButtonUp"
Style="{StaticResource pagerBorder}">
Command="{Binding NextCommand}"
Style="{StaticResource pagerButton}"
Visibility="{Binding NextVisibility}">
<Image
Width="16"
Height="16"
@ -310,16 +337,16 @@
<Style TargetType="Image">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=nameNext}" Value="True">
<Setter Property="Source" Value="{DynamicResource ToRightHoverDrawingImage}" />
<Setter Property="Source" Value="{StaticResource ToRightHoverDrawingImage}" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=nameNext}" Value="False">
<Setter Property="Source" Value="{DynamicResource ToRightDrawingImage}" />
<Setter Property="Source" Value="{StaticResource ToRightDrawingImage}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Border>
</Button>
</StackPanel>
</UserControl>

@ -1,8 +1,6 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Controls;
namespace DownKyi.ui.CustomControl
namespace DownKyi.CustomControl
{
/// <summary>
/// CustomPager.xaml 的交互逻辑
@ -12,264 +10,6 @@ namespace DownKyi.ui.CustomControl
public CustomPager()
{
InitializeComponent();
DataContext = this;
}
// Current修改的回调
public delegate void CurrentChangedHandler(int current);
public event CurrentChangedHandler CurrentChanged;
protected virtual void OnCurrentChanged(int current)
{
CurrentChanged?.Invoke(current);
}
// Count修改的回调
public delegate void CountChangedHandler(int current);
public event CountChangedHandler CountChanged;
protected virtual void OnCountChanged(int count)
{
CountChanged?.Invoke(count);
}
private int current;
public int Current
{
get
{
var value = GetValue(CurrentProperty);
if (value == null) { return 1; }
if ((int)value < 1)
{
return 1;
}
return (int)value;
}
set
{
SetValue(CurrentProperty, value);
if (Count > 0 && (value > Count || value < 1))
{
//throw new Exception("数值不在允许的范围内。");
}
else
{
current = value;
SetView();
OnCurrentChanged(current);
}
}
}
public static readonly DependencyProperty CurrentProperty =
DependencyProperty.Register(
"Current",
typeof(object),
typeof(CustomPager),
new PropertyMetadata(default, OnCurrentPropertyChanged));
private static void OnCurrentPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//CustomPager source = d as CustomPager;
}
//private int count;
public int Count
{
get
{
return (int)GetValue(CountProperty);
}
set
{
SetValue(CountProperty, value);
//if (value < Current || value < 1)
//{
// //throw new Exception("数值不在允许的范围内。");
//}
//else
//{
// count = value;
// if (count == 1) { Visibility = Visibility.Hidden; }
// else { Visibility = Visibility.Visible; }
// SetView();
// OnCountChanged(count);
//}
}
}
public static readonly DependencyProperty CountProperty =
DependencyProperty.Register(
"Count",
typeof(object),
typeof(CustomPager),
new PropertyMetadata(default, OnCountPropertyChanged));
private static void OnCountPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CustomPager source = d as CustomPager;
if ((int)e.NewValue < source.Current || (int)e.NewValue < 1)
{
//throw new Exception("数值不在允许的范围内。");
}
else
{
//source.Count = (int)e.NewValue;
if (source.Count == 1) { source.Visibility = Visibility.Hidden; }
else { source.Visibility = Visibility.Visible; }
source.SetView();
source.OnCountChanged(source.Count);
}
}
private void Previous_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Current -= 1;
}
private void First_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Current = 1;
}
private void PreviousSecond_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Current -= 2;
}
private void PreviousFirst_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Current -= 1;
}
private void Current_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{ }
private void NextFirst_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Current += 1;
}
private void NextSecond_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Current += 2;
}
private void Last_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Current = Count;
}
private void Next_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Current += 1;
}
/// <summary>
/// 控制显示,暴力实现,以后重构
/// </summary>
protected void SetView()
{
nameFirstText.Text = "1";
namePreviousSecondText.Text = (Current - 2).ToString();
namePreviousFirstText.Text = (Current - 1).ToString();
nameCurrentText.Text = Current.ToString();
nameNextFirstText.Text = (Current + 1).ToString();
nameNextSecondText.Text = (Current + 2).ToString();
nameLastText.Text = Count.ToString();
// 控制Current左边的控件
if (Current == 1)
{
namePrevious.Visibility = Visibility.Collapsed;
nameFirst.Visibility = Visibility.Collapsed;
nameLeftJump.Visibility = Visibility.Collapsed;
namePreviousSecond.Visibility = Visibility.Collapsed;
namePreviousFirst.Visibility = Visibility.Collapsed;
}
else if (Current == 2)
{
namePrevious.Visibility = Visibility.Visible;
nameFirst.Visibility = Visibility.Collapsed;
nameLeftJump.Visibility = Visibility.Collapsed;
namePreviousSecond.Visibility = Visibility.Collapsed;
namePreviousFirst.Visibility = Visibility.Visible;
}
else if (Current == 3)
{
namePrevious.Visibility = Visibility.Visible;
nameFirst.Visibility = Visibility.Collapsed;
nameLeftJump.Visibility = Visibility.Collapsed;
namePreviousSecond.Visibility = Visibility.Visible;
namePreviousFirst.Visibility = Visibility.Visible;
}
else if (Current == 4)
{
namePrevious.Visibility = Visibility.Visible;
nameFirst.Visibility = Visibility.Visible;
nameLeftJump.Visibility = Visibility.Collapsed;
namePreviousSecond.Visibility = Visibility.Visible;
namePreviousFirst.Visibility = Visibility.Visible;
}
else
{
namePrevious.Visibility = Visibility.Visible;
nameFirst.Visibility = Visibility.Visible;
nameLeftJump.Visibility = Visibility.Visible;
namePreviousSecond.Visibility = Visibility.Visible;
namePreviousFirst.Visibility = Visibility.Visible;
}
// 控制Current右边的控件
if (Current == Count)
{
nameNextFirst.Visibility = Visibility.Collapsed;
nameNextSecond.Visibility = Visibility.Collapsed;
nameRightJump.Visibility = Visibility.Collapsed;
nameLast.Visibility = Visibility.Collapsed;
nameNext.Visibility = Visibility.Collapsed;
}
else if (Current == Count - 1)
{
nameNextFirst.Visibility = Visibility.Visible;
nameNextSecond.Visibility = Visibility.Collapsed;
nameRightJump.Visibility = Visibility.Collapsed;
nameLast.Visibility = Visibility.Collapsed;
nameNext.Visibility = Visibility.Visible;
}
else if (Current == Count - 2)
{
nameNextFirst.Visibility = Visibility.Visible;
nameNextSecond.Visibility = Visibility.Visible;
nameRightJump.Visibility = Visibility.Collapsed;
nameLast.Visibility = Visibility.Collapsed;
nameNext.Visibility = Visibility.Visible;
}
else if (Current == Count - 3)
{
nameNextFirst.Visibility = Visibility.Visible;
nameNextSecond.Visibility = Visibility.Visible;
nameRightJump.Visibility = Visibility.Collapsed;
nameLast.Visibility = Visibility.Visible;
nameNext.Visibility = Visibility.Visible;
}
else
{
nameNextFirst.Visibility = Visibility.Visible;
nameNextSecond.Visibility = Visibility.Visible;
nameRightJump.Visibility = Visibility.Visible;
nameLast.Visibility = Visibility.Visible;
nameNext.Visibility = Visibility.Visible;
}
}
}
}

@ -0,0 +1,455 @@
using System.ComponentModel;
using System.Windows;
namespace DownKyi.CustomControl
{
public class CustomPagerViewModel : INotifyPropertyChanged
{
public CustomPagerViewModel(int current, int count)
{
Current = current;
Count = count;
SetView();
}
public event PropertyChangedEventHandler PropertyChanged;
// Current修改的回调
public delegate void CurrentChangedHandler(int current);
public event CurrentChangedHandler CurrentChanged;
protected virtual void OnCurrentChanged(int current)
{
CurrentChanged?.Invoke(current);
}
// Count修改的回调
public delegate void CountChangedHandler(int count);
public event CountChangedHandler CountChanged;
protected virtual void OnCountChanged(int count)
{
CountChanged?.Invoke(count);
}
#region 绑定属性
private Visibility visibility;
public Visibility Visibility
{
get { return visibility; }
set
{
visibility = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Visibility"));
}
}
private int count;
public int Count
{
get
{
return count;
}
set
{
if (value < Current || value < 1)
{
//throw new Exception("数值不在允许的范围内。");
System.Console.WriteLine(value.ToString());
}
else
{
count = value;
if (count == 1) { Visibility = Visibility.Hidden; }
else { Visibility = Visibility.Visible; }
//SetView();
OnCountChanged(count);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Count"));
}
}
}
private int current;
public int Current
{
get
{
if (current < 1) { current = 1; }
return current;
}
set
{
if (Count > 0 && (value > Count || value < 1))
{
//throw new Exception("数值不在允许的范围内。");
}
else
{
current = value;
//SetView();
OnCurrentChanged(current);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Current"));
}
}
}
private int first;
public int First
{
get { return first; }
set
{
first = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("First"));
}
}
private int previousSecond;
public int PreviousSecond
{
get { return previousSecond; }
set
{
previousSecond = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PreviousSecond"));
}
}
private int previousFirst;
public int PreviousFirst
{
get { return previousFirst; }
set
{
previousFirst = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PreviousFirst"));
}
}
private int nextFirst;
public int NextFirst
{
get { return nextFirst; }
set
{
nextFirst = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("NextFirst"));
}
}
private int nextSecond;
public int NextSecond
{
get { return nextSecond; }
set
{
nextSecond = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("NextSecond"));
}
}
// 控制Current左边的控件
private Visibility previousVisibility;
public Visibility PreviousVisibility
{
get { return previousVisibility; }
set
{
previousVisibility = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PreviousVisibility"));
}
}
private Visibility firstVisibility;
public Visibility FirstVisibility
{
get { return firstVisibility; }
set
{
firstVisibility = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FirstVisibility"));
}
}
private Visibility leftJumpVisibility;
public Visibility LeftJumpVisibility
{
get { return leftJumpVisibility; }
set
{
leftJumpVisibility = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("LeftJumpVisibility"));
}
}
private Visibility previousSecondVisibility;
public Visibility PreviousSecondVisibility
{
get { return previousSecondVisibility; }
set
{
previousSecondVisibility = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PreviousSecondVisibility"));
}
}
private Visibility previousFirstVisibility;
public Visibility PreviousFirstVisibility
{
get { return previousFirstVisibility; }
set
{
previousFirstVisibility = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PreviousFirstVisibility"));
}
}
// 控制Current右边的控件
private Visibility nextFirstVisibility;
public Visibility NextFirstVisibility
{
get { return nextFirstVisibility; }
set
{
nextFirstVisibility = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("NextFirstVisibility"));
}
}
private Visibility nextSecondVisibility;
public Visibility NextSecondVisibility
{
get { return nextSecondVisibility; }
set
{
nextSecondVisibility = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("NextSecondVisibility"));
}
}
private Visibility rightJumpVisibility;
public Visibility RightJumpVisibility
{
get { return rightJumpVisibility; }
set
{
rightJumpVisibility = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("RightJumpVisibility"));
}
}
private Visibility lastVisibility;
public Visibility LastVisibility
{
get { return lastVisibility; }
set
{
lastVisibility = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("LastVisibility"));
}
}
private Visibility nextVisibility;
public Visibility NextVisibility
{
get { return nextVisibility; }
set
{
nextVisibility = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("NextVisibility"));
}
}
#endregion
private MyDelegateCommand previousCommand;
public MyDelegateCommand PreviousCommand => previousCommand ?? (previousCommand = new MyDelegateCommand(PreviousExecuted));
public void PreviousExecuted(object obj)
{
Current -= 1;
SetView();
}
private MyDelegateCommand firstCommand;
public MyDelegateCommand FirstCommand => firstCommand ?? (firstCommand = new MyDelegateCommand(FirstExecuted));
public void FirstExecuted(object obj)
{
Current = 1;
SetView();
}
private MyDelegateCommand previousSecondCommand;
public MyDelegateCommand PreviousSecondCommand => previousSecondCommand ?? (previousSecondCommand = new MyDelegateCommand(PreviousSecondExecuted));
public void PreviousSecondExecuted(object obj)
{
Current -= 2;
SetView();
}
private MyDelegateCommand previousFirstCommand;
public MyDelegateCommand PreviousFirstCommand => previousFirstCommand ?? (previousFirstCommand = new MyDelegateCommand(PreviousFirstExecuted));
public void PreviousFirstExecuted(object obj)
{
Current -= 1;
SetView();
}
private MyDelegateCommand nextFirstCommand;
public MyDelegateCommand NextFirstCommand => nextFirstCommand ?? (nextFirstCommand = new MyDelegateCommand(NextFirstExecuted));
public void NextFirstExecuted(object obj)
{
Current += 1;
SetView();
}
private MyDelegateCommand nextSecondCommand;
public MyDelegateCommand NextSecondCommand => nextSecondCommand ?? (nextSecondCommand = new MyDelegateCommand(NextSecondExecuted));
public void NextSecondExecuted(object obj)
{
Current += 2;
SetView();
}
private MyDelegateCommand lastCommand;
public MyDelegateCommand LastCommand => lastCommand ?? (lastCommand = new MyDelegateCommand(LastExecuted));
public void LastExecuted(object obj)
{
Current = Count;
SetView();
}
private MyDelegateCommand nextCommand;
public MyDelegateCommand NextCommand => nextCommand ?? (nextCommand = new MyDelegateCommand(NextExecuted));
public void NextExecuted(object obj)
{
Current += 1;
SetView();
}
/// <summary>
/// 控制显示,暴力实现,以后重构
/// </summary>
private void SetView()
{
First = 1;
PreviousSecond = Current - 2;
PreviousFirst = Current - 1;
NextFirst = Current + 1;
NextSecond = Current + 2;
// 控制Current左边的控件
if (Current == 1)
{
PreviousVisibility = Visibility.Collapsed;
FirstVisibility = Visibility.Collapsed;
LeftJumpVisibility = Visibility.Collapsed;
PreviousSecondVisibility = Visibility.Collapsed;
PreviousFirstVisibility = Visibility.Collapsed;
}
else if (Current == 2)
{
PreviousVisibility = Visibility.Visible;
FirstVisibility = Visibility.Collapsed;
LeftJumpVisibility = Visibility.Collapsed;
PreviousSecondVisibility = Visibility.Collapsed;
PreviousFirstVisibility = Visibility.Visible;
}
else if (Current == 3)
{
PreviousVisibility = Visibility.Visible;
FirstVisibility = Visibility.Collapsed;
LeftJumpVisibility = Visibility.Collapsed;
PreviousSecondVisibility = Visibility.Visible;
PreviousFirstVisibility = Visibility.Visible;
}
else if (Current == 4)
{
PreviousVisibility = Visibility.Visible;
FirstVisibility = Visibility.Visible;
LeftJumpVisibility = Visibility.Collapsed;
PreviousSecondVisibility = Visibility.Visible;
PreviousFirstVisibility = Visibility.Visible;
}
else
{
PreviousVisibility = Visibility.Visible;
FirstVisibility = Visibility.Visible;
LeftJumpVisibility = Visibility.Visible;
PreviousSecondVisibility = Visibility.Visible;
PreviousFirstVisibility = Visibility.Visible;
}
// 控制Current右边的控件
if (Current == Count)
{
NextFirstVisibility = Visibility.Collapsed;
NextSecondVisibility = Visibility.Collapsed;
RightJumpVisibility = Visibility.Collapsed;
LastVisibility = Visibility.Collapsed;
NextVisibility = Visibility.Collapsed;
}
else if (Current == Count - 1)
{
NextFirstVisibility = Visibility.Visible;
NextSecondVisibility = Visibility.Collapsed;
RightJumpVisibility = Visibility.Collapsed;
LastVisibility = Visibility.Collapsed;
NextVisibility = Visibility.Visible;
}
else if (Current == Count - 2)
{
NextFirstVisibility = Visibility.Visible;
NextSecondVisibility = Visibility.Visible;
RightJumpVisibility = Visibility.Collapsed;
LastVisibility = Visibility.Collapsed;
NextVisibility = Visibility.Visible;
}
else if (Current == Count - 3)
{
NextFirstVisibility = Visibility.Visible;
NextSecondVisibility = Visibility.Visible;
RightJumpVisibility = Visibility.Collapsed;
LastVisibility = Visibility.Visible;
NextVisibility = Visibility.Visible;
}
else
{
NextFirstVisibility = Visibility.Visible;
NextSecondVisibility = Visibility.Visible;
RightJumpVisibility = Visibility.Visible;
LastVisibility = Visibility.Visible;
NextVisibility = Visibility.Visible;
}
}
}
}

@ -0,0 +1,85 @@
using System;
using System.Windows.Input;
namespace DownKyi.CustomControl
{
/// <summary>
/// 绑定命令的工具类
/// </summary>
public class MyDelegateCommand : ICommand
{
/// <summary>
/// 检查命令是否可以执行的事件在UI事件发生导致控件状态或数据发生变化时触发
/// </summary>
public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null)
{
CommandManager.RequerySuggested += value;
}
}
remove
{
if (_canExecute != null)
{
CommandManager.RequerySuggested -= value;
}
}
}
/// <summary>
/// 判断命令是否可以执行的方法
/// </summary>
private Func<object, bool> _canExecute;
/// <summary>
/// 命令需要执行的方法
/// </summary>
private Action<object> _execute;
/// <summary>
/// 创建一个命令
/// </summary>
/// <param name="execute">命令要执行的方法</param>
public MyDelegateCommand(Action<object> execute) : this(execute, null)
{
}
/// <summary>
/// 创建一个命令
/// </summary>
/// <param name="execute">命令要执行的方法</param>
/// <param name="canExecute">判断命令是否能够执行的方法</param>
public MyDelegateCommand(Action<object> execute, Func<object, bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
/// <summary>
/// 判断命令是否可以执行
/// </summary>
/// <param name="parameter">命令传入的参数</param>
/// <returns>是否可以执行</returns>
public bool CanExecute(object parameter)
{
if (_canExecute == null) return true;
return _canExecute(parameter);
}
/// <summary>
/// 执行命令
/// </summary>
/// <param name="parameter"></param>
public void Execute(object parameter)
{
if (_execute != null && CanExecute(parameter))
{
_execute(parameter);
}
}
}
}

@ -83,6 +83,8 @@
<Compile Include="CustomControl\CustomPager.xaml.cs">
<DependentUpon>CustomPager.xaml</DependentUpon>
</Compile>
<Compile Include="CustomControl\CustomPagerViewModel.cs" />
<Compile Include="CustomControl\MyDelegateCommand.cs" />
<Compile Include="CustomControl\GifImage.cs" />
<Compile Include="Events\MessageEvent.cs" />
<Compile Include="Events\NavigationEvent.cs" />
@ -231,8 +233,8 @@
<DependentUpon>ViewVideoDetail.xaml</DependentUpon>
</Compile>
<Page Include="CustomControl\CustomPager.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Languages\en_US.xaml">
<Generator>MSBuild:Compile</Generator>

@ -1,5 +1,6 @@
using DownKyi.Events;
using DownKyi.Images;
using DownKyi.CustomControl;
using DownKyi.Utils;
using DownKyi.ViewModels.PageViewModels;
using DownKyi.ViewModels.UserSpace;
@ -61,6 +62,13 @@ namespace DownKyi.ViewModels
set => SetProperty(ref currentPage, value);
}
private CustomPagerViewModel pager;
public CustomPagerViewModel Pager
{
get => pager;
set => SetProperty(ref pager, value);
}
#endregion
public ViewPublicationViewModel(IEventAggregator eventAggregator) : base(eventAggregator)
@ -110,12 +118,20 @@ namespace DownKyi.ViewModels
if (!(parameter is TabHeader tabHeader)) { return; }
// 页面选择
CountPage = (int)Math.Ceiling(double.Parse(tabHeader.SubTitle) / VideoNumberInPage);
CurrentPage = 1;
Pager = new CustomPagerViewModel(1, (int)Math.Ceiling(double.Parse(tabHeader.SubTitle) / VideoNumberInPage));
Pager.CurrentChanged += OnCurrentChanged_Pager;
Pager.CountChanged += OnCountChanged_Pager;
}
#endregion
private void OnCountChanged_Pager(int count) { }
private void OnCurrentChanged_Pager(int current)
{
Console.WriteLine(current);
}
/// <summary>
/// 初始化页面数据
/// </summary>
@ -163,8 +179,9 @@ namespace DownKyi.ViewModels
SelectTabId = TabHeaders.IndexOf(selectTab);
// 页面选择
CountPage = (int)Math.Ceiling(double.Parse(selectTab.SubTitle) / VideoNumberInPage);
CurrentPage = 1;
Pager = new CustomPagerViewModel(1, (int)Math.Ceiling(double.Parse(selectTab.SubTitle) / VideoNumberInPage));
Pager.CurrentChanged += OnCurrentChanged_Pager;
Pager.CountChanged += OnCountChanged_Pager;
}
}

@ -2,7 +2,7 @@
x:Class="DownKyi.Views.ViewPublication"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:DownKyi.ui.CustomControl"
xmlns:custom="clr-namespace:DownKyi.CustomControl"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
@ -111,13 +111,10 @@
Style="{StaticResource CheckBoxStyle}" />
<custom:CustomPager
x:Name="namePager"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Count="{Binding CountPage, Mode=TwoWay}"
Current="{Binding CurrentPage, Mode=TwoWay}"
DataContext="{Binding}" />
DataContext="{Binding Pager}" />
<Button
Grid.Column="2"

Loading…
Cancel
Save