Wpf拖放ItemControls

时间:2015-04-06 10:34:50

标签: c# wpf drag-and-drop

我需要拖放所有elemet在itemcontrol项目中,只有当药物的第一个Label时。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
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;
using GongSolutions.Wpf;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;
namespace DragAndDropBehavior
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
/// 
public class DataStruct
{
    public int number { get; set; }
    public string Name { get; set; }
}
[Serializable]
public class TestSer
{
    public ObservableCollection<DataStruct> Items { get; set; }
    public string Names { get; set; }
    public TestSer()
    {
        Items = new ObservableCollection<DataStruct>();
        Items.Add(new DataStruct {number=1,Name="First"});
        Items.Add(new DataStruct {number=2,Name="Two"});
        Items.Add(new DataStruct {number=3,Name="Three"});
    }
}


public partial class MainWindow : Window
{
    TestSer Some;
    public MainWindow()
    {
        this.DataContext = new TestSer();
        InitializeComponent();
    }
    public object tempSendre { get; set; }
    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
    }

    private void Label_Drop(object sender, DragEventArgs e)
    {
        (tempSendre as Label).Content = ((Label)sender).Content;
        (sender as Label).Content = e.Data.GetData(DataFormats.Text);
    }

    private void Label_MouseDown(object sender, MouseButtonEventArgs e)
    {
        tempSendre = sender;
        Label lbl = (Label)sender;
        DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Copy);
    }
}
}

Xaml代码。

<Window x:Class="DragAndDropBehavior.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
    Title="MainWindow" Height="350" Width="525" Closing="Window_Closing">
<StackPanel>
    <ItemsControl ItemsSource="{Binding Items}" x:Name="TestControl">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="2" Columns="4" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="#FF000000" BorderThickness="1,1,1,1" CornerRadius="8,8,8,8" Margin="5">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                            <RowDefinition></RowDefinition>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition></ColumnDefinition>
                            <ColumnDefinition></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Label Grid.Row="0" Content="{Binding Name}" Grid.ColumnSpan="2" Drop="Label_Drop" MouseDown="Label_MouseDown" AllowDrop="True"></Label>
                        <Label Grid.Row="1" Content="{Binding number}"></Label>
                        <Button Grid.Row="2" Content="{Binding Name}"></Button>
                    </Grid>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>

起初我使用NuGet的Gong,但它不正确。 * *

0 个答案:

没有答案