c# - UWP:- 拖动项目时如何创建拖动 UI(指示要放置项目的位置)?

标签 c# xaml uwp

我正在尝试创建一个类似于下面屏幕截图的拖动用户界面。如果我将 ListView 中的AllowDrop设置为“true”,它会自动打开空间,为拖动的项目提供放置空间。但是,我更热衷于提供一个用户界面,指示“放在此处”而不是空格。

当我移动到拖动的项目上时,如何创建如下所示的带有占位符指示的内容?

有什么想法吗?

enter image description here

enter image description here

enter image description here

最佳答案

How can I create of something like in the below with placeholders indication when I move over the dragged item?

根据您的要求,您可以使用ListView Footer要实现这个功能,并处理FooterCollapsed属性(property)在DragOverDrop事件处理程序。请引用以下代码。

private  void TargetListView_DragOver(object sender, DragEventArgs e)
{
    // Our list only accepts text
    e.AcceptedOperation = (e.DataView.Contains(StandardDataFormats.Text)) ? DataPackageOperation.Copy : DataPackageOperation.None;
    VisualStateManager.GoToState(this, "Inside", true);
}


private async void TargetListView_Drop(object sender, DragEventArgs e)
{

    if (e.DataView.Contains(StandardDataFormats.Text))
    {

        var def = e.GetDeferral();
        var s = await e.DataView.GetTextAsync();
        var items = s.Split('\n');
        foreach (var item in items)
        {
            _selection.Add(item);
        }
        e.AcceptedOperation = DataPackageOperation.Copy;
        VisualStateManager.GoToState(this, "Outside", true);
        def.Complete();
    }
}

Xaml

<ListView
    x:Name="TargetListView"
    Grid.Row="2"
    Grid.Column="1"
    Margin="8,4"
    AllowDrop="True"
    CanDragItems="True"
    CanReorderItems="True"
    DragItemsCompleted="TargetListView_DragItemsCompleted"
    DragItemsStarting="TargetListView_DragItemsStarting"
    DragOver="TargetListView_DragOver"
    Drop="TargetListView_Drop"
    >
    <ListView.Footer>
        <Border Background="DarkGray" Opacity="0.8">
            <TextBlock
                x:Name="Footer"
                Height="44"
                Margin="0,0,0,0"
                HorizontalAlignment="Center"
                VerticalAlignment="Bottom"
                FontSize="25"
                Text="Please Place your item"
                TextAlignment="Center"
                Visibility="Collapsed"
                />
        </Border>
    </ListView.Footer>
</ListView>


<VisualStateManager.VisualStateGroups>
    <VisualStateGroup x:Name="DragDropGroup">
        <VisualState x:Name="Outside" />
        <VisualState x:Name="Inside">
            <VisualState.Setters>
                <Setter Target="Footer.Visibility" Value="Visible" />
            </VisualState.Setters>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

下面发布了用户的声音,如果有人认为这有帮助,请投票。

Allowing custom air drop UI while dragging items of a ListView

关于c# - UWP:- 拖动项目时如何创建拖动 UI(指示要放置项目的位置)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56851104/

相关文章:

c# - 如何在 C# 中生成 "system modal dialog"?

xaml - ContentDialog 不滚动

wpf - 如何在 WPF 中的 DataGrid 标题下方绘制分隔线

c# - 在对图像使用 UniformToFill 时,如何在 C# 或 XAML 中控制裁剪

c# - 这对于使用 IDisposable 处理对象是否正确

c# - 服务帐户无法从 Windows 证书存储加载 X509Certificate

c# - 如何在c#中使用UWP下载文件

c# - 我可以在 Hololens 中录制也使用相机的应用程序的视频吗?有没有办法两全其美?

c# - 什么是 Template10 导航服务的 NavigationServices.FirstOrDefault()?

c# - RhinoMocks - 在模拟界面上引发事件失败