在列表框之间拖放

时间:2019-07-03 08:08:55

标签: c# wpf drag-and-drop listbox

我想在列表框之间拖放项目。一种位于MainWindow中,另一种位于UserControl中。制表符控件项是动态编码的。([VideoListing],[AddTab,AddItem])我很好奇这是否可行,然后为我提供指导。

这就是我想要的。 enter image description here

这是我的代码

Mainwindow.xaml

<TabControl x:Name="scenarioCB" ItemsSource="{Binding}" Grid.Row="1" HorizontalAlignment="Stretch" Margin="5,0,5,5"  VerticalAlignment="Stretch" SelectionChanged="ScenarioCB_SelectionChanged">
            <TabControl.ItemTemplate>
                <DataTemplate DataType="local:AddTab">
                    <TextBlock Text="{Binding Header}"/>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate DataType="local:AddTab">
                    <ListBox x:Name="listBox" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" ItemsSource="{Binding Data}" AllowDrop="True" Drop="ListBox_Drop" DragEnter="ListBox_DragEnter" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Visible">
                        <ListBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel IsItemsHost="True"/>
                            </ItemsPanelTemplate>
                        </ListBox.ItemsPanel>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <MediaElement Margin="3" Source="{Binding Path}" Height="64" Stretch="Uniform" IsMuted="True"/>
                                    <TextBlock Margin="3" Text="{Binding Name}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>

Mainwindow.xaml.cs

var tabs = new ObservableCollection<AddTab>();
            for (int i = 0; i < DateListCount; i++)
            {
                var tab = new AddTab();
                tab = new AddTab() { Header = DateList[i] + " - " + TimeList[i] };
                tab.Data.Add(new AddData() { TIME = TimeList[i] });
                Console.WriteLine("i = {0}, Header = {1}, Time = {2}", i, DateList[i], TimeList[i]);
                tabs.Add(tab);
            }
            DataContext = tabs;

AddTab.cs

class AddTab
{
    public string Header { get; set; }
    public string Time { get; set; }
    public ObservableCollection<AddData> Data { get; } = new ObservableCollection<AddData>();
}

AddData.cs

class AddData
{
    public string NAME { get; set; }
    public string PATH { get; set; }
}

VideoPanel.xaml

<ListBox Grid.Row="0" x:Name="listBox" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" AllowDrop="True" Drop="ListBox_Drop" DragEnter="ListBox_DragEnter" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <MediaElement Margin="3" Source="{Binding Path}" Height="64" Stretch="Uniform" IsMuted="True"/>
                    <TextBlock Margin="3" Text="{Binding Name}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

VideoPanel.xaml.cs

list.Add(new VideoListing()
            {
                Name = file_name,
                Path = file,
            });

VideoListing.cs

class VideoListing
{
    public string Name { get; set; }
    public string Path { get; set; }
}

如果您有示例,请发布链接。

  • 在列表框(MainWindow-UserControl)之间拖放

  • 塔比特词是动态编码的

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。通过DragDrop.DoDragDrop使用

当我按下鼠标左键运行DragDrop.DoDragDrop(DependencyObject dragSourse,对象数据,DragDropEffects allowedEffects)时。当我将一个项目放到另一个列表框中时,生成ListBox_Drop事件。在LisetBox_Drop事件中,只需将数据从DragEventArgs中拉出即可。

列表框-拖动

private void ListBox_MouseMove(object sender, MouseEventArgs e)
    {
        DataObject dataObj = new DataObject();
        dynamic Booth = listBox.SelectedItem as dynamic;
        if (sender is ListBox && e.LeftButton == MouseButtonState.Pressed)
        {
            dataObj.SetData("Name", Booth.Name);
            dataObj.SetData("Path", Booth.Path);
            DragDrop.DoDragDrop(listBox, dataObj, DragDropEffects.Move);
        }
    }

列表框-删除

private void ListBox_Drop(object sender, DragEventArgs e)
    {
        string name = (string)e.Data.GetData("Name");
        string path = (string)e.Data.GetData("Path");
    }