https://download.csdn.net/download/sinat_30224769/13778570
界面
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d" x:Name="uc"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Window.Resources>
<ControlTemplate x:Key="ssss1" TargetType="{x:Type Control}">
<Grid>
<Button Content="{Binding Content}" Tag="{Binding .}" Height="50" Width="200"
/>
<Grid
Tag="{Binding .}"
Background="Transparent"
PreviewMouseLeftButtonDown="sp_PreviewMouseLeftButtonDown"
PreviewMouseLeftButtonUp="sp_PreviewMouseLeftButtonUp"
PreviewMouseMove="sp_PreviewMouseMove"
Drop="sp_Drop"
DragEnter="sp_DragEnter"
/>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="ssss2" TargetType="{x:Type Control}">
<TextBox Text="{Binding Content}" Tag="{Binding .}" SelectedText=""
AutoWordSelection="True"
LostFocus="TextBox_LostFocus"
KeyUp="TextBox_KeyUp" Height="50" Width="200" />
</ControlTemplate>
</Window.Resources>
<Grid>
<ItemsControl x:Name="sp22" ItemsSource="{Binding ItemSource,ElementName=uc}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Control Tag="{Binding .}" >
<Control.Style>
<Style TargetType="{x:Type Control}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsEditing}" Value="false">
<Setter Property="Template" Value="{StaticResource ssss1}"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsEditing}" Value="true">
<Setter Property="Template" Value="{StaticResource ssss2}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Control.Style>
</Control>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel
AllowDrop="True"
/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Window>
后台
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.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 WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
List<ItemInfo> list = new List<ItemInfo>();
for (int i = 0; i < 10; i++)
{
list.Add(new ItemInfo() { Content = i.ToString() });
}
ItemSource = new ObservableCollection<ItemInfo>(list);
}
public class ItemInfo : GalaSoft.MvvmLight.ObservableObject
{
private string _Content;
public string Content { get => _Content; set => Set(ref _Content, value); }
private bool _IsEditing;
public bool IsEditing { get => _IsEditing; set => Set(ref _IsEditing, value); }
}
public ObservableCollection<ItemInfo> ItemSource
{
get { return (ObservableCollection<ItemInfo>)GetValue(ItemSourceProperty); }
set { SetValue(ItemSourceProperty, value); }
}
// Using a DependencyProperty as the backing store for ItemSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ItemSourceProperty =
DependencyProperty.Register("ItemSource", typeof(ObservableCollection<ItemInfo>), typeof(MainWindow), new PropertyMetadata(null));
//按下并拖动
//按下且
private bool _isDown;
private bool _isDragging;
private int index_From;
private void sp_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_isDown = true;
var button = e.Source as FrameworkElement;
var das = button.DataContext as ItemInfo;
index_From = ItemSource.IndexOf(das);
if (e.ClickCount == 2)
{
_isDragging = false;
_isDown = false;
das.IsEditing = true;
}
}
private void sp_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_isDown = false;
_isDragging = false;
}
private void sp_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (_isDown)
{
var noediting = !ItemSource.Any(x => x.IsEditing);
if (_isDragging == false && noediting)
{
_isDragging = true;
UIElement _dummyDragSource = new UIElement();
if (!ItemSource.Any(x => x.IsEditing))
{
DragDrop.DoDragDrop(_dummyDragSource, new DataObject("UIElement", e.Source, true), DragDropEffects.Move);
}
}
}
}
private void sp_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("UIElement"))
{
e.Effects = DragDropEffects.Move;
}
}
private void sp_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("UIElement"))
{
FrameworkElement droptarget = e.Source as FrameworkElement;
var das = droptarget.DataContext as ItemInfo;
int droptargetIndex = ItemSource.IndexOf(das);
if (droptargetIndex != -1)
{
var objFrom = ItemSource[index_From];
this.ItemSource.Remove(objFrom);
this.ItemSource.Insert(droptargetIndex, objFrom);
}
_isDown = false;
_isDragging = false;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = (sender as FrameworkElement);
var tag = button.Tag as ItemInfo;
MessageBox.Show(tag.Content);
}
private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
FrameworkElement button = sender as FrameworkElement;
var dc = button.Tag as ItemInfo;
dc.IsEditing = true;
}
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
FrameworkElement textBox = sender as FrameworkElement;
var dc = textBox.Tag as ItemInfo;
if (dc.IsEditing)
{
dc.IsEditing = false;
}
}
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
FrameworkElement textBox = sender as FrameworkElement;
var dc = textBox.Tag as ItemInfo;
if (dc.IsEditing)
{
dc.IsEditing = false;
}
}
}
}
}