效果:
项目结构:
控件前台代码:
<UserControl x:Class="ServerConnect.ServiceConnectControl"
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:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:ServerConnect"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800">
<UserControl.Resources>
<SolidColorBrush x:Key="Button.Static.Background"
Color="Transparent" />
<SolidColorBrush x:Key="Button.Static.Border"
Color="Transparent" />
<SolidColorBrush x:Key="Button.MouseOver.Background"
Color="#FFBEE6FD" />
<SolidColorBrush x:Key="Button.MouseOver.Border"
Color="#FF3C7FB1" />
<SolidColorBrush x:Key="Button.Pressed.Background"
Color="#FFC4E5F6" />
<SolidColorBrush x:Key="Button.Pressed.Border"
Color="#FF2C628B" />
<SolidColorBrush x:Key="Button.Disabled.Background"
Color="#FFF4F4F4" />
<SolidColorBrush x:Key="Button.Disabled.Border"
Color="#FFADB2B5" />
<SolidColorBrush x:Key="Button.Disabled.Foreground"
Color="#FF838383" />
<Style x:Key="ComputerIconStyle"
TargetType="Path">
<Setter Property="Width"
Value="30" />
<Setter Property="Height"
Value="30" />
<Setter Property="Data"
Value="M832 769.6H192c-66.24 0-120-53.76-120-120V208c0-66.24 53.76-120 120-120h640c66.24 0 120 53.76 120 120v441.6c0 66.24-53.76 120-120 120zM192 136c-39.68 0-72 32.32-72 72v441.6c0 39.68 32.32 72 72 72h640c39.68 0 72-32.32 72-72V208c0-39.68-32.32-72-72-72H192z
M104 602.88h816v48h-816zM424 936c-13.28 0-24-10.72-24-24v-158.4c0-13.28 10.72-24 24-24s24 10.72 24 24V912c0 13.28-10.72 24-24 24z
M760 936h-496c-13.28 0-24-10.72-24-24s10.72-24 24-24h496c13.28 0 24 10.72 24 24s-10.72 24-24 24z
M600 936c-13.28 0-24-10.72-24-24v-158.4c0-13.28 10.72-24 24-24s24 10.72 24 24V912c0 13.28-10.72 24-24 24z
M232 365.76c-13.28 0-24-10.72-24-24V278.4c0-39.36 32.32-71.52 72-71.52h60c13.28 0 24 10.72 24 24s-10.72 24-24 24H280c-13.28 0-24 10.56-24 23.52v63.36c0 13.28-10.72 24-24 24z" />
</Style>
<Style x:Key="PreviousPageButtonStyle"
TargetType="{x:Type Button}">
<Setter Property="Height"
Value="30" />
<Setter Property="Width"
Value="20" />
<Setter Property="Background"
Value="{StaticResource Button.Static.Background}" />
<Setter Property="BorderBrush"
Value="{StaticResource Button.Static.Border}" />
<Setter Property="Foreground"
Value="White" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="RenderTransformOrigin"
Value="0.5,0.5" />
<Setter Property="HorizontalContentAlignment"
Value="Center" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
<Setter Property="Padding"
Value="1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<Grid>
<Ellipse x:Name="ellipse"
Fill="#FF90F0FC"
Opacity="0"
RenderTransformOrigin="0.5,0.5">
<Ellipse.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="0.8"
ScaleY="0.8" />
<SkewTransform />
<RotateTransform />
<TranslateTransform />
</TransformGroup>
</Ellipse.RenderTransform>
<Ellipse.Effect>
<BlurEffect KernelType="Box"
Radius="6" />
</Ellipse.Effect>
</Ellipse>
<Path Fill="{TemplateBinding Foreground}"
Stretch="Uniform"
Height="20"
Width="10"
Data="M369.728 512l384.768-384.704a48.64 48.64 0 0 0 0.896-68.8 48.64 48.64 0 0 0-68.736 0.96L269.44 476.736a48.704 48.704 0 0 0-11.136 17.344c-1.024 2.304-1.024 4.736-1.472 7.04-0.896 3.648-2.048 7.168-2.048 10.88 0 3.712 1.152 7.232 1.984 10.88 0.512 2.368 0.512 4.8 1.472 7.04a48.704 48.704 0 0 0 11.136 17.344l417.216 417.28a48.576 48.576 0 0 0 68.736 0.96 48.576 48.576 0 0 0-0.896-68.736L369.728 512z" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted"
Value="true">
<Setter Property="BorderBrush"
TargetName="border"
Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>
<Trigger Property="IsMouseOver"
Value="true">
<Setter Property="Opacity"
TargetName="ellipse"
Value="0.8" />
</Trigger>
<Trigger Property="IsPressed"
Value="true">
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform ScaleX="0.9"
ScaleY="0.9" />
</Setter.Value>
</Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(UIElement.Opacity)">
<EasingDoubleKeyFrame KeyTime="0"
Value="0.8" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(UIElement.Opacity)">
<EasingDoubleKeyFrame KeyTime="0"
Value="0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsEnabled"
Value="false">
<Setter Property="Background"
TargetName="border"
Value="{StaticResource Button.Disabled.Background}" />
<Setter Property="BorderBrush"
TargetName="border"
Value="{StaticResource Button.Disabled.Border}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="NextPageButtonStyle"
TargetType="{x:Type Button}">
<Setter Property="Height"
Value="30" />
<Setter Property="Width"
Value="20" />
<Setter Property="Background"
Value="{StaticResource Button.Static.Background}" />
<Setter Property="BorderBrush"
Value="{StaticResource Button.Static.Border}" />
<Setter Property="Foreground"
Value="White" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="RenderTransformOrigin"
Value="0.5,0.5" />
<Setter Property="HorizontalContentAlignment"
Value="Center" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
<Setter Property="Padding"
Value="1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<Grid>
<Ellipse x:Name="ellipse"
Fill="#FF90F0FC"
Opacity="0"
RenderTransformOrigin="0.5,0.5">
<Ellipse.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleX="0.8"
ScaleY="0.8" />
<SkewTransform />
<RotateTransform />
<TranslateTransform />
</TransformGroup>
</Ellipse.RenderTransform>
<Ellipse.Effect>
<BlurEffect KernelType="Box"
Radius="6" />
</Ellipse.Effect>
</Ellipse>
<Path Fill="{TemplateBinding Foreground}"
Stretch="Uniform"
Height="20"
Width="10"
Data="M723.696401 533.102744c0.486519-0.973037 1.337926-1.824445 1.702815-2.797482 8.514075-17.757928 5.716593-39.651265-9.365483-53.881934L372.30835 151.307281c-18.730966-17.757928-48.28697-16.906521-66.044898 1.824445-17.757928 18.730966-16.906521 48.28697 1.824445 66.044898l308.452785 291.789524L309.304193 807.012709c-18.609336 17.879558-19.095855 47.435562-1.216296 66.044898 9.122224 9.487112 21.406818 14.352298 33.569783 14.352298 11.676446 0 23.352892-4.378667 32.353486-13.136002l340.563012-328.278418c0.608148-0.608148 0.851408-1.581185 1.581185-2.189334 0.486519-0.486519 0.973037-0.851408 1.581185-1.337926C720.53403 539.670745 721.871956 536.265115 723.696401 533.102744L723.696401 533.102744zM723.696401 533.102744" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted"
Value="true">
<Setter Property="BorderBrush"
TargetName="border"
Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
</Trigger>
<Trigger Property="IsMouseOver"
Value="true">
<Setter Property="Opacity"
TargetName="ellipse"
Value="0.8" />
</Trigger>
<Trigger Property="IsPressed"
Value="true">
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform ScaleX="0.9"
ScaleY="0.9" />
</Setter.Value>
</Setter>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(UIElement.Opacity)">
<EasingDoubleKeyFrame KeyTime="0"
Value="0.8" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetName="ellipse"
Storyboard.TargetProperty="(UIElement.Opacity)">
<EasingDoubleKeyFrame KeyTime="0"
Value="0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsEnabled"
Value="false">
<Setter Property="Background"
TargetName="border"
Value="{StaticResource Button.Disabled.Background}" />
<Setter Property="BorderBrush"
TargetName="border"
Value="{StaticResource Button.Disabled.Border}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Viewbox Stretch="Fill">
<Canvas x:Name="Canvas_Root"
Height="600"
Width="960"
Loaded="Canvas_Root_Loaded">
<Grid x:Name="Grid_MainIcon"
Canvas.Top="10"
Canvas.Left="450">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="5" />
<RowDefinition Height="auto" />
<RowDefinition Height="5" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0"
Text="主服务器"
HorizontalAlignment="Center"
FontSize="16"
FontWeight="Bold"
Foreground="White" />
<Path x:Name="Path_SeverceIcon"
Grid.Row="2"
Stretch="Uniform"
Fill="White"
VerticalAlignment="Center"
Height="50"
Width="50"
Data="M886.053 383.408h-756.151c-51.967 0-67.751 26.649-67.751 61.328v131.943c0 50.951 26.569 61.436 67.751 61.436h756.148c42.032 0 67.751-20.234 67.751-61.328v-131.943c0.001-43.703-19.074-61.436-67.749-61.436v0zM894.639 579.132h-771.169v-135.728h771.17l-0.001 135.728z M724.29 534.678h90.123c6.202 0 11.291-4.773 11.291-10.628v-21.255c0-5.858-5.036-10.628-11.291-10.628h-90.123c-6.202 0-11.241 4.718-11.241 10.628v21.255c0 5.858 5.036 10.628 11.241 10.628v0z M188.128 492.659h42.51v42.509h-42.51v-42.509z M274.159 491.672h42.51v42.509h-42.51v-42.509z M362.337 492.659h42.51v42.509h-42.51v-42.509z M447.287 493.72h42.51v42.511h-42.51v-42.511z M886.053 703.301h-756.151c-51.967 0-67.751 26.649-67.751 61.328v131.943c0 50.95 26.569 61.435 67.751 61.435h756.148c42.032 0 67.751-20.236 67.751-61.328v-131.943c0.001-43.698-19.074-61.435-67.749-61.435v0zM894.639 899.023h-771.169v-135.728h771.17l-0.001 135.728z M724.29 854.565h90.123c6.202 0 11.291-4.771 11.291-10.627v-21.258c0-5.858-5.036-10.627-11.291-10.627h-90.123c-6.202 0-11.241 4.718-11.241 10.627v21.258c0 5.858 5.036 10.627 11.241 10.627v0z M188.128 812.547h42.51v42.51h-42.51v-42.51z M274.159 811.561h42.51v42.509h-42.51v-42.509z M362.337 812.547h42.51v42.51h-42.51v-42.51z M447.287 813.611h42.51v42.51h-42.51v-42.51z M886.053 64.585h-756.151c-51.967 0-67.751 26.649-67.751 61.328v131.943c0 50.951 26.569 61.436 67.751 61.436h756.148c42.032 0 67.751-20.236 67.751-61.328v-131.943c0.001-43.706-19.074-61.436-67.749-61.436v0zM894.639 260.307h-771.169v-135.728h771.17l-0.001 135.728z M724.29 214.785h90.123c6.202 0 11.291-4.771 11.291-10.627v-21.258c0-5.858-5.036-10.627-11.291-10.627h-90.123c-6.202 0-11.241 4.718-11.241 10.627v21.258c0 5.858 5.036 10.627 11.241 10.627v0z M188.128 172.771h42.51v42.509h-42.51v-42.509z M274.159 171.782h42.51v42.51h-42.51v-42.51z M362.337 172.771h42.51v42.509h-42.51v-42.509z M447.287 173.832h42.51v42.51h-42.51v-42.51z" />
<StackPanel Grid.Row="4"
HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal">
<TextBlock Text="已连接:"
Foreground="White"
FontWeight="Bold" />
<TextBlock x:Name="TextBlock_Num"
FontWeight="Bold"
Foreground="White" />
</StackPanel>
</StackPanel>
</Grid>
<ListBox x:Name="ListBox_ConnectList"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Top"
SelectionChanged="ListBox_ConnectList_SelectionChanged"
Background="Transparent"
BorderBrush="Transparent"
MinHeight="100"
MinWidth="100"
Canvas.Left="70"
Canvas.Top="300">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"
IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels"
Value="True" />
<Setter Property="Padding"
Value="4,1" />
<Setter Property="RenderTransformOrigin"
Value="0.5,0.5" />
<Setter Property="Width"
Value="40" />
<Setter Property="Margin"
Value="40,0,0,0" />
<Setter Property="HorizontalContentAlignment"
Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
<Setter Property="VerticalContentAlignment"
Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
<Setter Property="BorderBrush"
Value="Transparent" />
<Setter Property="BorderThickness"
Value="1" />
<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">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="5" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Border Grid.Row="0">
<Path Fill="White"
Stretch="Uniform"
Style="{StaticResource ComputerIconStyle}" />
</Border>
<TextBlock Margin="0"
Foreground="White"
FontSize="10"
Grid.Row="2"
TextTrimming="CharacterEllipsis"
ToolTip="{Binding Text}"
Text="{Binding ClientComputerIP}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver"
Value="True" />
</MultiTrigger.Conditions>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive"
Value="False" />
<Condition Property="IsSelected"
Value="True" />
</MultiTrigger.Conditions>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive"
Value="True" />
<Condition Property="IsSelected"
Value="True" />
</MultiTrigger.Conditions>
<Setter Property="RenderTransform">
<Setter.Value>
<ScaleTransform ScaleX="0.9"
ScaleY="0.9" />
</Setter.Value>
</Setter>
</MultiTrigger>
<Trigger Property="IsEnabled"
Value="False">
<Setter Property="TextElement.Foreground"
TargetName="Bd"
Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
<Trigger Property="IsMouseOver"
Value="true">
<Setter TargetName="Bd"
Property="ToolTip">
<Setter.Value>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Text="用户名:" />
<TextBlock Text="{Binding UserName}"
Grid.Row="0"
Grid.Column="1" />
<TextBlock Grid.Row="1"
Grid.Column="0"
Text="操作时间:"></TextBlock>
<TextBlock Text="{Binding Opertime}"
Grid.Row="1"
Grid.Column="1"></TextBlock>
<TextBlock Grid.Row="2"
Grid.Column="0"
Text="API名称:" />
<TextBlock Text="{Binding ApiInterfaceName}"
Grid.Row="2"
Grid.Column="1" />
<TextBlock Grid.Row="3"
Grid.Column="0"
Text="IP:" />
<TextBlock Text="{Binding ClientComputerIP}"
Grid.Row="3"
Grid.Column="1" />
<TextBlock Text="地点: "
Grid.Row="4"
Grid.Column="0" />
<TextBlock Text="{Binding LocationName}"
Grid.Row="4"
Grid.Column="1" />
<TextBlock Text="方法名: "
Grid.Row="5"
Grid.Column="0"></TextBlock>
<TextBlock Text="{Binding MethodName}"
Grid.Row="5"
Grid.Column="1"></TextBlock>
<TextBlock Text="CounterTime: "
Grid.Row="6"
Grid.Column="0" />
<TextBlock Text="{Binding CounterTime}"
Grid.Row="6"
Grid.Column="1" />
<TextBlock Grid.Column="0"
Grid.Row="7"
Text="ResponseData" />
<TextBlock Text="{Binding ResponseData}"
Grid.Row="7"
Grid.Column="1" />
</Grid>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button x:Name="Button_PreviousPage"
Canvas.Left="50"
Canvas.Top="305"
Visibility="Hidden"
Click="Button_PreviousPage_Click"
Style="{StaticResource PreviousPageButtonStyle}" />
<Button x:Name="Button_NextPage"
Canvas.Right="25"
Canvas.Top="305"
Visibility="Hidden"
Click="Button_NextPage_Click"
Style="{StaticResource NextPageButtonStyle}" />
<!--<StackPanel x:Name="itemLayout" Orientation="Horizontal" Height="330" Canvas.Top="120" Width="800"/>-->
</Canvas>
</Viewbox>
</UserControl>
控件后台代码:
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace ServerConnect
{
/// <summary>
/// ServiceConnectControl.xaml 的交互逻辑
/// </summary>
public partial class ServiceConnectControl : UserControl
{
private int _pageNum = 0;
private readonly int _pageSize = 10;
private int _currentPage = 0;
private int _collectionIndex = 0;
private Line line;
private readonly List<Line> _lines = new List<Line>();
public ServiceConnectControl()
{
InitializeComponent();
}
/// <summary>
/// item大小
/// </summary>
private double _itemSize;
/// <summary>
/// 元素的位置集合
/// </summary>
private List<Point> _itemPoints;
/// <summary>
/// 起点位置
/// </summary>
private Point _startPoint;
#region 自定依赖义属性
#region ItemsSource
public static readonly DependencyProperty ItemsSourceDependencyProperty = DependencyProperty.Register(
"ItemsSource",
typeof(List<Model.CountUserData>),
typeof(ServiceConnectControl),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(ItemsSourcePropertyChangedCallback)
)
);
public List<Model.CountUserData> ItemsSource
{
get => (List<Model.CountUserData>)GetValue(ItemsSourceDependencyProperty);
set => SetValue(ItemsSourceDependencyProperty, value);
}
private static void ItemsSourcePropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (sender != null && sender.GetType() == typeof(ServiceConnectControl))
{
ServiceConnectControl sc = (ServiceConnectControl)sender;
sc.Init();
sc.GenerateItem(e.NewValue as List<Model.CountUserData>);
}
}
#endregion
#endregion
/// <summary>
/// 初始化
/// </summary>
private void Init()
{
_itemPoints = new List<Point>();
_startPoint = Grid_MainIcon.TranslatePoint(new Point(), Canvas_Root);
_startPoint = new Point(_startPoint.X + Grid_MainIcon.ActualWidth / 2, _startPoint.Y + Grid_MainIcon.ActualHeight);
_itemSize = 40;
}
/// <summary>
/// 生成item
/// </summary>
private void GenerateItem(List<Model.CountUserData> itemsSource)
{
if (itemsSource == null)
{
return;
}
TextBlock_Num.Text = itemsSource.Count.ToString();
if (itemsSource.Count > 10)
{
ListBox_ConnectList.ItemsSource = itemsSource.Skip(_collectionIndex).Take(_pageSize).ToList();
_currentPage = 1;
DrawLine(ListBox_ConnectList.Items.Count);
}
else
{
ListBox_ConnectList.ItemsSource = itemsSource;
_currentPage = 1;
DrawLine(ListBox_ConnectList.Items.Count);
}
GetPageNumber(itemsSource);
}
/// <summary>
/// 获取虚线的终点集合
/// </summary>
/// <param name="num"></param>
private void GetItemPoints(int num)
{
_itemPoints.Add(new Point((num * (_itemSize + 40) + 130), 300));
}
/// <summary>
/// 画虚线的方法
/// </summary>
/// <param name="count">个数</param>
private void DrawLine(int count)
{
if (Canvas_Root.Children.Contains(line))
{
for (int i = 0; i < _lines.Count; i++)
{
Canvas_Root.Children.Remove(_lines[i]);
}
}
for (int i = 0; i < count; i++)
{
GetItemPoints(i);
line = new Line
{
X1 = _startPoint.X,
X2 = _itemPoints[i].X,
Y1 = _startPoint.Y,
Y2 = _itemPoints[i].Y,
Stroke = Brushes.White,
StrokeThickness = 3,
StrokeDashCap = PenLineCap.Triangle,
StrokeDashArray = new DoubleCollection() { 2, 4 } //2为单个线段的长度,2为线段间的间距
};
Canvas_Root.Children.Add(line);
_lines.Add(line);
//虚线流动
LineMoveThreadStart(line);
}
}
/// <summary>
/// 得到分页的页数
/// </summary>
/// <param name="itemsource"></param>
/// <returns></returns>
private int GetPageNumber(List<Model.CountUserData> itemsource)
{
if (itemsource.Count % _pageSize == 0)
{
_pageNum = itemsource.Count / _pageSize;
return _pageNum;
}
else
{
_pageNum = (itemsource.Count / _pageSize) + 1;
return _pageNum;
}
}
/// <summary>
/// 显示翻页按钮
/// </summary>
private void ShowButton()
{
if (ItemsSource.Count > 10)
{
Button_PreviousPage.Visibility = Visibility.Visible;
Button_NextPage.Visibility = Visibility.Visible;
}
}
/// <summary>
/// 隐藏按钮
/// </summary>
private void HideButton()
{
Button_PreviousPage.Visibility = Visibility.Hidden;
Button_NextPage.Visibility = Visibility.Hidden;
}
/// <summary>
/// 虚线的移动
/// </summary>
/// <param name="line"></param>
private void LineMoveThreadStart(Line line)
{
Thread P_th = new Thread(() =>
{
while (true)//开始无限循环
{
Dispatcher.BeginInvoke(//在窗体线程中执行相关代码
System.Windows.Threading.DispatcherPriority.Normal,//设置线程优先级
(ThreadStart)(() =>
{
line.StrokeDashOffset -= 10;
}));
Thread.Sleep(500);//线程挂起
}
})
{
IsBackground = true//设置线程为后台线程
};
P_th.Start();//线程开始
}
#region 事件
private void Button_PreviousPage_Click(object sender, RoutedEventArgs e)
{
if (_currentPage > 1)
{
_currentPage -= 1;
_collectionIndex -= 10;
ListBox_ConnectList.ItemsSource = ItemsSource.Skip(_collectionIndex).Take(_pageSize).ToList();
DrawLine(ListBox_ConnectList.Items.Count);
}
else
{
System.Windows.MessageBox.Show("已经到最前了");
}
}
private void Button_NextPage_Click(object sender, RoutedEventArgs e)
{
if (_currentPage < _pageNum)
{
_currentPage += 1;
_collectionIndex += 10;
ListBox_ConnectList.ItemsSource = ItemsSource.Skip(_collectionIndex).Take(_pageSize).ToList();
DrawLine(ListBox_ConnectList.Items.Count);
}
else
{
System.Windows.MessageBox.Show("已经到最后了");
}
}
private void Canvas_Root_Loaded(object sender, RoutedEventArgs e)
{
ShowButton();
}
#endregion
private void ListBox_ConnectList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
}
Model代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServerConnect.Model
{
public class CountUserData
{
private string _opertime;
private string _guid;
private string _apiInterfaceName;
private string _clientComputerIP;
private string _userName;
private string _locationName;
private string _methodName;
private double _counterTime;
private string _responseData;
private double _month;
private string _parameterContents;
private string _lanIp = "";
private string _modelName;
public string LanIp
{
get => _lanIp;
set => _lanIp = value;
}
public string ModelName
{
get => _modelName;
set => _modelName = value;
}
public double Month
{
get => _month;
set => _month = value;
}
public string ParameterContents
{
get => _parameterContents;
set => _parameterContents = value;
}
public string Opertime
{
get => _opertime;
set => _opertime = value;
}
public string Guid
{
get => _guid;
set => _guid = value;
}
public string ApiInterfaceName
{
get => _apiInterfaceName;
set => _apiInterfaceName = value;
}
public string ClientComputerIP
{
get => _clientComputerIP;
set => _clientComputerIP = value;
}
public string UserName
{
get => _userName;
set => _userName = value;
}
public string LocationName
{
get => _locationName;
set => _locationName = value;
}
public string MethodName
{
get => _methodName;
set => _methodName = value;
}
public double CounterTime
{
get => _counterTime;
set => _counterTime = value;
}
public string ResponseData
{
get => _responseData;
set => _responseData = value;
}
}
}
主界面前台代码:
<Window x:Class="ServerConnect.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ServerConnect"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid Background="Black">
<local:ServiceConnectControl ItemsSource="{Binding CountUserDatas}"/>
</Grid>
</Window>
主界面后台代码:
using ServerConnect.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ServerConnect
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
List<Model.CountUserData> _countUserDatas = new List<Model.CountUserData>();
public MainWindow()
{
InitializeComponent();
InitData();
this.DataContext = this;
}
public List<CountUserData> CountUserDatas { get => _countUserDatas; set => _countUserDatas = value; }
void InitData()
{
for (int i = 0; i < 6; i++)
{
CountUserDatas.Add(new CountUserData
{
ClientComputerIP = string.Format("10.35.1.22{0}", i)
});
}
}
}
}