WPF 实现鼠标拖动来控制 控件排序(ListView 做示例)

该文章演示了如何在WPF应用中使用gong-wpf-dragdrop库实现ListView的拖动排序功能。首先介绍了基本的XAML布局,然后展示了后端代码设置数据绑定。接着,通过三步(安装扩展包、引用命名空间、修改ListView属性)详细解释了启用拖放操作的过程。
摘要由CSDN通过智能技术生成

示例效果 前端

<Window
    x:Class="WPF_实现拖动排序_ListView_做示例_.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:local="clr-namespace:WPF_实现拖动排序_ListView_做示例_"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <ListView Grid.Column="1" ItemsSource="{Binding MyProperty, FallbackValue=123456789}" />
        <Grid Grid.Column="0" Background="WhiteSmoke" />
    </Grid>
</Window>

后端:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;  MyProperty = new ObservableCollection<string>() 
            {  "s",
                "s1",
                "s2"  };
        }
        private ObservableCollection<string> myVar;
        public ObservableCollection<string> MyProperty
        {
            get { return myVar; }
            set { myVar = value; }
        }
    }

绑定最终效果

当前这个排序顺序我们是无法用鼠标拖动来实现改变它的排列顺序,接下来就将一步步实现此效果。

第一步  安装gong-wpf-dragdrop扩展包

第二步  引用命名空间 xmlns:dd="urn:gong-wpf-dragdrop"

第三步  修改ListView

  <ListView
            Grid.Column="1"
            dd:DragDrop.DropHandler="{Binding}"
            dd:DragDrop.DropTargetAdornerBrush="{DynamicResource PrimaryThemeColor}"
            dd:DragDrop.IsDragSource="True"
            dd:DragDrop.IsDropTarget="True"
            dd:DragDrop.UseDefaultDragAdorner="False"
            ItemsSource="{Binding MyProperty, FallbackValue=123456789}" />

鼠标拖动效果实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WPF 中的 DataGrid 和 ListView 都没有内置的分页控件,但可以通过一些方法来实现分页功能。 一种实现方式是使用 CollectionView,它是用于对集合进行排序、过滤和分组的类。可以使用 CollectionViewSource 创建 CollectionView,并在 XAML 中绑定到分页控件中。 以下是一个示例,其中使用 CollectionViewSource 来创建 CollectionView,并将其绑定到 ListView 控件中: ```xml <Grid> <Grid.Resources> <CollectionViewSource x:Key="cvs" Source="{Binding Items}" PageSize="10"/> </Grid.Resources> <ListView ItemsSource="{Binding Source={StaticResource cvs}}" /> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> <Button Content="Prev" Command="{Binding Source={StaticResource cvs}, Path=PageUpCommand}" /> <TextBlock Text="{Binding Source={StaticResource cvs}, Path=PageIndex}" /> <Button Content="Next" Command="{Binding Source={StaticResource cvs}, Path=PageDownCommand}" /> </StackPanel> </Grid> ``` 在上面的示例中,CollectionViewSource 用于创建 CollectionView,并使用 PageSize 属性来指定每页的项数。ListView 控件绑定到 CollectionViewSource 中的 CollectionView。 同时,使用按钮来切换分页,每个按钮都绑定到 CollectionViewSource 中的 PageUpCommand 和 PageDownCommand 命令,以便在前一页和后一页之间切换。 还有其他的实现方式,例如手动分页和使用第三方分页控件等等。具体实现方式可以根据项目需求来选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学软件开发的猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值