fyf 2 lat temu
rodzic
commit
b87c5d50fc
4 zmienionych plików z 390 dodań i 322 usunięć
  1. +29
    -0
      WPFDemo/TheListBox.xaml
  2. +356
    -0
      WPFDemo/TheListBox.xaml.cs
  3. +5
    -24
      WPFDemo/Window2.xaml
  4. +0
    -298
      WPFDemo/Window2.xaml.cs

+ 29
- 0
WPFDemo/TheListBox.xaml Wyświetl plik

@@ -0,0 +1,29 @@
<UserControl x:Class="WPFDemo.TheListBox"
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:WPFDemo"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<ListView x:Name="listView"
ItemsSource="{Binding ViewItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Background="{x:Null}" Foreground="White"
PreviewMouseMove="ListView_PreviewMouseMove"
PreviewMouseLeftButtonUp="lisbox_PreviewMouseLeftButtonUp">
<ListBox.ItemTemplate>
<ItemContainerTemplate>
<Border x:Name="border" Tag="border" BorderBrush="Aquamarine" MouseLeftButtonDown="Border_MouseLeftButtonDown" BorderThickness="1" Padding="5" Background="#FF18888A">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Margin="10,0,10,0" Grid.Column="0" Text="{Binding Name,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
<TextBlock Grid.Column="1" Text="{Binding Ph,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
</Grid>
</Border>
</ItemContainerTemplate>
</ListBox.ItemTemplate>
</ListView>
</UserControl>

+ 356
- 0
WPFDemo/TheListBox.xaml.cs Wyświetl plik

@@ -0,0 +1,356 @@
using Microsoft.Toolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFDemo
{
/// <summary>
/// fyf 创建测试案例
/// TheListBox.xaml 的交互逻辑
/// </summary>
public partial class TheListBox : UserControl
{
/// <summary>
/// 数据model
/// </summary>
public ListBoxDataModel TheListBoxModel = new ListBoxDataModel();
public TheListBox()
{
InitializeComponent();
this.DataContext = TheListBoxModel;
TheListBoxModel.ViewItems.Add(new ItemModel { Name = "张三", Ph = "125486545" });
TheListBoxModel.ViewItems.Add(new ItemModel { Name = "李四", Ph = "125486545" });
TheListBoxModel.ViewItems.Add(new ItemModel { Name = "王麻子", Ph = "125486545" });
TheListBoxModel.ViewItems.Add(new ItemModel { Name = "二货", Ph = "125486545" });
TheListBoxModel.ViewItems.Add(new ItemModel { Name = "张三1", Ph = "125486545" });
TheListBoxModel.ViewItems.Add(new ItemModel { Name = "李四2", Ph = "125486545" });
TheListBoxModel.ViewItems.Add(new ItemModel { Name = "王麻子3", Ph = "125486545" });
}
#region 移动事件
/// <summary>
/// 当前拖动子控件流
/// </summary>
private UIElement ChildElement;
/// <summary>
/// 当前拖拽子控件Item
/// </summary>
private ListBoxItem ChildListBoxItem;
/// <summary>
/// 是否已经按下
/// </summary>
private bool isDown = false;
/// <summary>
/// 当前拖动的Pop窗体
/// </summary>
private Popup DropPopup = null;
/// <summary>
/// 鼠标按下事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
try
{
//isDown变量是防止多次操作。
if (isDown)
{
isDown = false;
return;
}

ChildElement = (UIElement)sender;
ChildElement.CaptureMouse();//设置了鼠标捕获,这样它可以不受到其它控件的影响。
ChildListBoxItem = Utils.FindVisualParent<ListBoxItem>(VisualTreeHelper.HitTest(ChildElement, e.GetPosition(ChildElement)).VisualHit);

//创建一个Pop,表明拖拽开始
CreatePopup(ChildElement, e);

isDown = true;
}
catch (Exception ex)
{

}
}
/// <summary>
/// 鼠标抬起事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lisbox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
try
{
//鼠标未按下返回
if (!isDown) return;

isDown = false;

//关闭Pop窗体
if (this.DropPopup != null)
{
this.DropPopup.IsOpen = false;
this.DropPopup.Child = null;
this.DropPopup = null;
}

//蒙层关闭,表明结束拖拽
MoveListBoxStyle(null, false);

//当控件具有鼠标捕获的话,则释放该捕获。
ChildElement.ReleaseMouseCapture();
}
catch (Exception ex)
{

}
}
/// <summary>
/// 鼠标移动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListView_PreviewMouseMove(object sender, MouseEventArgs e)
{
try
{
if (isDown == false) return;

if (e.LeftButton != MouseButtonState.Pressed)
lisbox_PreviewMouseLeftButtonUp(null, null);

Point ptLeftUp = new Point(0, 0);
Point ptRightDown = new Point(this.ActualWidth, this.ActualHeight);

ptLeftUp = this.PointToScreen(ptLeftUp);
ptRightDown = this.PointToScreen(ptRightDown);

double y = e.GetPosition(this).Y;
double x = e.GetPosition(this).X;

if (DropPopup != null)//下面两句是设置Popup控件的位置除以2是想让鼠标在它的中心
{
DropPopup.HorizontalOffset = ptLeftUp.X + x - ((FrameworkElement)ChildElement).ActualWidth / 2;
DropPopup.VerticalOffset = ptLeftUp.Y + y - ((FrameworkElement)ChildElement).ActualHeight / 2;
}
MoveListBoxStyle(e,true);
}
catch (Exception ex)
{
}
}
/// <summary>
/// 移动效果
/// </summary>
/// <param name="boxItem"></param>
private void MoveListBoxStyle(MouseEventArgs e,bool isBool)
{
try
{
if (isBool)//为真,根据鼠标位置设置行透明度和显示状态
{
//移动到某行减轻某行 暗黑
foreach (ListBoxItem item in Utils.FindVisualChildren<ListBoxItem>(listView))
{
if (item != ChildListBoxItem)//这就是其他控件
{
double item_width = item.ActualWidth;
double item_height = item.ActualHeight;
double item_x = e.GetPosition(item).X;
double item_y = e.GetPosition(item).Y;
double Child_x = e.GetPosition(ChildElement).X;
double Child_y = e.GetPosition(ChildElement).Y;

if (item_y <= item_height && item_y > 0 && item_x > 0 && item_x <= item_width)//鼠标进入哪一行,则将那一行变灰
{
item.Opacity = 0.5;
int lao_index = TheListBoxModel.ViewItems.IndexOf(item.Content as ItemModel);
int new_index = TheListBoxModel.ViewItems.IndexOf(ChildListBoxItem.Content as ItemModel);
TheListBoxModel.ViewItems.Move(lao_index, new_index);
}
else //鼠标没在哪一行,则保持原状
{
item.Opacity = 1;
}
}
else
{
item.Visibility = Visibility.Hidden;
}
}
}
else//为假 恢复所有行透明度和显示状态
{
//移动到某行减轻某行 暗黑
foreach (ListBoxItem item in Utils.FindVisualChildren<ListBoxItem>(listView))
{
item.Opacity = 1;
item.Visibility = Visibility.Visible;
}
}
}
catch (Exception ex)
{
}
}
/// <summary>
/// 创建浮动窗口
/// </summary>
/// <param name="dragElement"></param>
/// <param name="e"></param>
private void CreatePopup(Visual dragElement, MouseButtonEventArgs e)
{
//使用PointToScreen函数可以将点转换为屏幕坐标
//首先获取当前窗体的左上角和右下角两点的坐标
Point ptLeftUp = new Point(0, 0);
//转换获取到这个窗口相对于屏幕两个坐标
ptLeftUp = this.PointToScreen(ptLeftUp);
//获取myGrid的实际宽高,主
double y = e.GetPosition(this).Y;
double x = e.GetPosition(this).X;

//拖拽Popup框
this.DropPopup = new Popup();
Border border = new Border();
border.Margin = new Thickness(0, 0, 8, 8);
DropShadowEffect effect = new DropShadowEffect();
effect.Opacity = 1;
effect.ShadowDepth = -14;
effect.BlurRadius = 9;
effect.Color = Color.FromArgb(100, 0, 0, 0);
border.Effect = effect;

//矩阵框
Rectangle r = new Rectangle();
r.Width = ((FrameworkElement)dragElement).ActualWidth;
r.Height = ((FrameworkElement)dragElement).ActualHeight;
r.Fill = new VisualBrush(dragElement);
border.Child = r;
this.DropPopup.Child = border;
DropPopup.AllowsTransparency = true;
DropPopup.HorizontalOffset = ptLeftUp.X + x - ((FrameworkElement)dragElement).ActualWidth / 2;
DropPopup.VerticalOffset = ptLeftUp.Y + y - ((FrameworkElement)dragElement).ActualHeight / 2;
this.DropPopup.IsOpen = true;
}
#endregion
}
/// <summary>
/// 当前项数据Model
/// </summary>
public class ListBoxDataModel : ObservableObject
{
private ObservableCollection<ItemModel> _ViewItems;
public ObservableCollection<ItemModel> ViewItems
{
get
{
return _ViewItems;
}
set
{
_ViewItems = value;
OnPropertyChanged("ViewItems");
}
}
public ListBoxDataModel()
{
ViewItems = new ObservableCollection<ItemModel>();
}
}
/// <summary>
/// 行数据Model
/// </summary>
public class ItemModel : ObservableObject
{
private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
private string _Ph;
public string Ph
{
get
{
return _Ph;
}
set
{
_Ph = value;
OnPropertyChanged("Ph");
}
}
}
/// <summary>
/// 帮助类
/// </summary>
internal static class Utils
{
/// <summary>
/// 根据子元素查找父元素
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static T FindVisualParent<T>(DependencyObject obj) where T : class
{
while (obj != null)
{
if (obj is T)
return obj as T;

obj = VisualTreeHelper.GetParent(obj);
}
return null;
}
/// <summary>
/// 查询子控件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}

foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
}
}

+ 5
- 24
WPFDemo/Window2.xaml Wyświetl plik

@@ -10,29 +10,10 @@
<Grid.Background>
<ImageBrush ImageSource="/bj.png"/>
</Grid.Background>
<ListView x:Name="lisbox" ItemsSource="{Binding ViewItems,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Background="{x:Null}" Foreground="White"
PreviewMouseMove="ListView_PreviewMouseMove"
PreviewMouseLeftButtonUp="lisbox_PreviewMouseLeftButtonUp">
<ListBox.ItemTemplate>
<ItemContainerTemplate>
<Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown" >
<Border BorderBrush="Aquamarine" BorderThickness="1" Padding="5">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Name,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
<TextBlock Grid.Column="1" Text="{Binding Ph,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"></TextBlock>
</Grid>
</Border>
</Grid>
</ItemContainerTemplate>
</ListBox.ItemTemplate>
</ListView>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<local:TheListBox HorizontalAlignment="Right" Grid.Row="1"></local:TheListBox>
</Grid>
</Window>

+ 0
- 298
WPFDemo/Window2.xaml.cs Wyświetl plik

@@ -23,307 +23,9 @@ namespace WPFDemo
/// </summary>
public partial class Window2 : Window
{
TestModel testModel=new TestModel();
public Window2()
{
InitializeComponent();
this.DataContext = testModel;


testModel.ViewItems.Add(new PageModel {Name="张三", Ph="125486545" });
testModel.ViewItems.Add(new PageModel { Name = "李四", Ph = "125486545" });
testModel.ViewItems.Add(new PageModel { Name = "王麻子", Ph = "125486545" });
testModel.ViewItems.Add(new PageModel { Name = "二货", Ph = "125486545" });
}

#region 移动事件
/// <summary>
/// 当前拖动子控件流
/// </summary>
private UIElement ChildElement;
/// <summary>
/// 是否已经按下
/// </summary>
private bool isDown = false;
/// <summary>
/// 按下记录初始鼠标点位
/// 鼠标相对与拖动子控件的位置(偏移量)
/// </summary>
private Point InitPoint;
/// <summary>
/// 当前拖动的Pop窗体
/// </summary>
private Popup DropPopup = null;


/// <summary>
/// 鼠标按下事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
try
{
//isDown变量是防止多次操作。
if (isDown)
{
isDown = false;
return;
}

ChildElement = (UIElement)sender;
ChildElement.CaptureMouse();//设置了鼠标捕获,这样它可以不受到其它控件的影响。

InitPoint= new Point(e.GetPosition(ChildElement).X, e.GetPosition(ChildElement).Y);

//加蒙层,表明已经开始拖动
//暂时未加

CreatePopup(ChildElement, e);

isDown = true;
}
catch (Exception ex)
{

}
}
/// <summary>
/// 鼠标抬起事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lisbox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
try
{
//鼠标未按下返回
if (!isDown) return;

isDown = false;

//关闭Pop窗体
if (this.DropPopup != null)
{
this.DropPopup.IsOpen = false;
this.DropPopup.Child = null;
this.DropPopup = null;
}

//蒙层关闭

ChildElement.ReleaseMouseCapture();//当控件具有鼠标捕获的话,则释放该捕获。

//double y = e.GetPosition(this).Y;//获取鼠标抬起时的控件的位置 Y值
//double start = 0.0;
//int row = 0;
//foreach (RowDefinition rd in myGrid.RowDefinitions)
//{
// start += rd.ActualHeight;
// if (y < start)
// {
// break;
// }
// row++;
//}
//double x = e.GetPosition(myGrid).X;//获取鼠标抬起时的控件的位置 X值
//double cstart = 0.0;
//int column = 0;
//foreach (ColumnDefinition cd in myGrid.ColumnDefinitions)
//{
// cstart += cd.ActualWidth;
// if (x < cstart)
// {
// break;
// }
// column++;
//}
//var initRow = Grid.GetRow(ultUE);//找到控件所在的行
//var initCol = Grid.GetColumn(ultUE);//找到控件所在的列
//UIElement uIElement = null;
//if (row != initRow || column != initCol)
//{
// uIElement = GetChildren(myGrid, row, column);
//}
//if (uIElement != null)
//{
// //下面是交换两个控件的位置 (需要先移除再加载)
// myGrid.Children.Remove(uIElement);
// Grid.SetColumn(ultUE, column);//指定控件在grid中哪行哪例
// Grid.SetRow(ultUE, row);
// myGrid.Children.Add(uIElement);
// Grid.SetColumn(uIElement, initCol);
// Grid.SetRow(uIElement, initRow);
//}
}
catch (Exception ex)
{

}
}
/// <summary>
/// 鼠标移动事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ListView_PreviewMouseMove(object sender, MouseEventArgs e)
{
try
{
if (isDown == false) return;

Point ptLeftUp = new Point(0, 0);
Point ptRightDown = new Point(this.ActualWidth, this.ActualHeight);

ptLeftUp = this.PointToScreen(ptLeftUp);
ptRightDown = this.PointToScreen(ptRightDown);

double y = e.GetPosition(lisbox).Y;
double x = e.GetPosition(lisbox).X;


if (DropPopup != null)
{
//下面两句是设置Popup控件的位置除以2是想让鼠标在它的中心
DropPopup.HorizontalOffset = ptLeftUp.X + x - ((FrameworkElement)ChildElement).ActualWidth / 2;
DropPopup.VerticalOffset = ptLeftUp.Y + y - ((FrameworkElement)ChildElement).ActualHeight / 2;
}

//double start = 0.0;
//int row = 0;
//foreach (RowDefinition rd in myGrid.RowDefinitions)
//{
// start += rd.ActualHeight;
// if (y < start)
// {
// break;
// }
// row++;
//}
//double cstart = 0.0;
//int column = 0;
//foreach (ColumnDefinition cd in myGrid.ColumnDefinitions)
//{
// cstart += cd.ActualWidth;
// if (x < cstart)
// {
// break;
// }
// column++;
//}
////下面的代码是当鼠标移动到哪个格子哪个格子就会亮起来。
//UIElement uIElement = GetChildren(myGrid, row, column);
//foreach (UIElement element in myGrid.Children)
//{
// if (element != ultUE && element != uIElement)
// {
// element.Opacity = moveOpacity;
// }
// else
// {
// element.Opacity = 1;
// }
//}
}
catch (Exception ex)
{

}
}
/// <summary>
/// 创建浮动窗口
/// </summary>
/// <param name="dragElement"></param>
/// <param name="e"></param>
private void CreatePopup(Visual dragElement, MouseButtonEventArgs e)
{
//使用PointToScreen函数可以将点转换为屏幕坐标
//首先获取当前窗体的左上角和右下角两点的坐标
Point ptLeftUp = new Point(0, 0);
//转换获取到这个窗口相对于屏幕两个坐标
ptLeftUp = this.PointToScreen(ptLeftUp);

//获取myGrid的实际宽高
double y = e.GetPosition(this).Y;
double x = e.GetPosition(this).X;


this.DropPopup = new Popup();
Border border = new Border();
border.Margin = new Thickness(0, 0, 8, 8);
DropShadowEffect effect = new DropShadowEffect();
effect.Opacity = 1;
effect.ShadowDepth = -14;
effect.BlurRadius = 9;
effect.Color = Color.FromArgb(100, 0, 0, 0);
border.Effect = effect;


Rectangle r = new Rectangle();
r.Width = ((FrameworkElement)dragElement).ActualWidth;
r.Height = ((FrameworkElement)dragElement).ActualHeight;
r.Fill = new VisualBrush(dragElement);
border.Child = r;
this.DropPopup.Child = border;
DropPopup.AllowsTransparency = true;
DropPopup.HorizontalOffset = ptLeftUp.X + x - ((FrameworkElement)dragElement).ActualWidth / 2;
DropPopup.VerticalOffset = ptLeftUp.Y + y - ((FrameworkElement)dragElement).ActualHeight / 2;

this.DropPopup.IsOpen = true;
}
#endregion
}

public class TestModel : ObservableObject
{
private ObservableCollection<PageModel> _pageModels;
public ObservableCollection<PageModel> ViewItems
{
get
{
return _pageModels;
}
set
{
_pageModels = value;
OnPropertyChanged("ViewItems");
}
}
public TestModel()
{
ViewItems=new ObservableCollection<PageModel>();
}
}

public class PageModel : ObservableObject
{
private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
private string _Ph;
public string Ph
{
get
{
return _Ph;
}
set
{
_Ph = value;
OnPropertyChanged("Ph");
}
}

}
}

Ładowanie…
Anuluj
Zapisz