Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Wpf空列表框模板+;拖放_C#_Wpf_Xaml_Drag And Drop_Listbox - Fatal编程技术网

C# Wpf空列表框模板+;拖放

C# Wpf空列表框模板+;拖放,c#,wpf,xaml,drag-and-drop,listbox,C#,Wpf,Xaml,Drag And Drop,Listbox,在回答之后,我将文本块垂直和水平对齐到中心 但是,当显示空模板时,我不能将项目拖放到列表框中,除非我将鼠标移到实际的文本块上。我希望能够在列表框内的任何位置拖动项目 main window.xaml <Window x:Class="EmptyListBoxWithDragAndDrop.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://

在回答之后,我将文本块垂直和水平对齐到中心

但是,当显示空模板时,我不能将项目拖放到列表框中,除非我将鼠标移到实际的文本块上。我希望能够在列表框内的任何位置拖动项目

main window.xaml

<Window x:Class="EmptyListBoxWithDragAndDrop.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:gong="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
    Title="MainWindow" Height="600" Width="800" WindowStartupLocation="CenterScreen">

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <ListBox Grid.Column="0" ItemsSource="{Binding Source}"
             gong:DragDrop.IsDragSource="True"/>

    <ListBox Grid.Column="1" ItemsSource="{Binding Target}"
             AllowDrop="True" gong:DragDrop.IsDropTarget="True" gong:DragDrop.DropHandler="{Binding}">
        <ListBox.Style>
            <Style  TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}">
                <Style.Triggers>
                    <Trigger Property="HasItems" Value="False">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <TextBlock Text="Drag items from left ListBox" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.Style>
    </ListBox>
</Grid>

ViewModel.cs

using GongSolutions.Wpf.DragDrop;
using System.Collections.ObjectModel;

namespace EmptyListBoxWithDragAndDrop
{
public class ViewModel : IDropTarget
{
    public ViewModel()
    {
        Source = new ObservableCollection<string>();
        Target = new ObservableCollection<string>();

        Source.Add("Item 1");
        Source.Add("Item 2");
        Source.Add("Item 3");
        Source.Add("Item 4");
        Source.Add("Item 5");
        Source.Add("Item 6");
        Source.Add("Item 7");
    }

    public ObservableCollection<string> Source { get; private set; }
    public ObservableCollection<string> Target { get; private set; }

    public void DragOver(IDropInfo dropInfo)
    {
        if (dropInfo.Data is string)
            dropInfo.Effects = System.Windows.DragDropEffects.Copy;
    }

    public void Drop(IDropInfo dropInfo)
    {
        if (dropInfo.Data is string)
        {
            Target.Add((string)dropInfo.Data);
        }
    }
}
}
使用GongSolutions.Wpf.DragDrop;
使用System.Collections.ObjectModel;
命名空间EmptyListBoxWithDragAndDrop
{
公共类ViewModel:IDropTarget
{
公共视图模型()
{
Source=新的ObservableCollection();
目标=新的ObservableCollection();
来源。添加(“第1项”);
来源。添加(“第2项”);
来源。添加(“第3项”);
来源。添加(“第4项”);
来源。添加(“第5项”);
来源。添加(“第6项”);
来源。添加(“第7项”);
}
公共ObservableCollection源{get;private set;}
公共ObservableCollection目标{get;private set;}
公共无效DragOver(IDropInfo dropInfo)
{
if(dropInfo.Data为字符串)
dropInfo.Effects=System.Windows.DragDropEffects.Copy;
}
公共作废删除(IDropInfo dropInfo)
{
if(dropInfo.Data为字符串)
{
Target.Add((字符串)dropInfo.Data);
}
}
}
}

我在用lib。有人知道如何解决这个问题吗?

您应该为自定义模板提供一个背景,以允许在整个客户端大小上删除目标

<ControlTemplate>
    <Grid Background="{TemplateBinding Background}">
        <TextBlock Text="Drag items from left ListBox" VerticalAlignment="Center" HorizontalAlignment="Center"/>
    </Grid>
</ControlTemplate>


希望这有帮助

您应该为自定义模板提供一个背景,以允许在整个客户端大小上删除目标

<ControlTemplate>
    <Grid Background="{TemplateBinding Background}">
        <TextBlock Text="Drag items from left ListBox" VerticalAlignment="Center" HorizontalAlignment="Center"/>
    </Grid>
</ControlTemplate>

希望这有帮助