我如何知道用户是否已放弃拖放?
How do I know if the user has given up on drag and drop?
我希望用户将 .txt 文件拖放到字段中。我为此创建了一个边框。我写了一个有用的 c# 代码来检查拖放状态。以下应用程序代码了解用户何时拖放文件。
我可以理解为用户将文件带到了现场。那么我如何判断用户是否从该区域删除了文件?
Xaml代码
<Border BorderThickness="1,1,1,1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.ColumnSpan="5"
Grid.RowSpan="5"
Width="500"
Opacity="0.5"
Background="LightGray"
Height="200"
helper:DropFilesBehaviorExtension.IsEnabled ="True">
<Border.Style>
<Style TargetType="Border">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding DragDropVisibility}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Border.BorderBrush>
<DrawingBrush Viewport="0,0,8,8" ViewportUnits="Absolute" TileMode="Tile">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="Black">
<GeometryDrawing.Geometry>
<GeometryGroup>
<RectangleGeometry Rect="0,0,50,50" />
<RectangleGeometry Rect="50,50,50,50" />
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Border.BorderBrush>
<TextBlock Text="Drag and drop the file you want to view onto the screen!"
FontSize="18"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
DropFilesBehavisorExtension
public class DropFilesBehaviorExtension
{
public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached(
"IsEnabled", typeof(bool), typeof(DropFilesBehaviorExtension), new FrameworkPropertyMetadata(default(bool), OnPropChanged)
{
BindsTwoWayByDefault = false,
});
private static void OnPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!(d is FrameworkElement fe))
throw new InvalidOperationException();
if ((bool)e.NewValue)
{
fe.AllowDrop = true;
fe.Drop += OnDrop;
fe.PreviewDragOver += OnPreviewDragOver;
}
else
{
fe.AllowDrop = false;
fe.Drop -= OnDrop;
fe.PreviewDragOver -= OnPreviewDragOver;
}
}
private static void OnPreviewDragOver(object sender, DragEventArgs e)
{
// Border opacity will be raised on drag.
e.Effects = DragDropEffects.Move;
e.Handled = true;
}
private static void OnDrop(object sender, DragEventArgs e)
{
var dataContext = ((FrameworkElement)sender).DataContext;
if (!(dataContext is IFilesDropped filesDropped))
{
if (dataContext != null)
Trace.TraceError($"Binding error, '{dataContext.GetType().Name}' doesn't implement '{nameof(IFilesDropped)}'.");
return;
}
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
return;
if (e.Data.GetData(DataFormats.FileDrop) is string[] files)
filesDropped.OnFilesDropped(files);
}
public static void SetIsEnabled(DependencyObject element, bool value)
{
element.SetValue(IsEnabledProperty, value);
}
public static bool GetIsEnabled(DependencyObject element)
{
return (bool)element.GetValue(IsEnabledProperty);
}
}
public interface IFilesDropped
{
void OnFilesDropped(string[] files);
}
DragLeave 功能解决了我的问题。一旦鼠标拖动离开所需区域,就会触发该功能。
fe.DragLeave += Fe_DragLeave;
private static void Fe_DragLeave(object sender, DragEventArgs e)
{
}
我希望用户将 .txt 文件拖放到字段中。我为此创建了一个边框。我写了一个有用的 c# 代码来检查拖放状态。以下应用程序代码了解用户何时拖放文件。
我可以理解为用户将文件带到了现场。那么我如何判断用户是否从该区域删除了文件?
Xaml代码
<Border BorderThickness="1,1,1,1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.ColumnSpan="5"
Grid.RowSpan="5"
Width="500"
Opacity="0.5"
Background="LightGray"
Height="200"
helper:DropFilesBehaviorExtension.IsEnabled ="True">
<Border.Style>
<Style TargetType="Border">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding DragDropVisibility}" Value="True">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Border.BorderBrush>
<DrawingBrush Viewport="0,0,8,8" ViewportUnits="Absolute" TileMode="Tile">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="Black">
<GeometryDrawing.Geometry>
<GeometryGroup>
<RectangleGeometry Rect="0,0,50,50" />
<RectangleGeometry Rect="50,50,50,50" />
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Border.BorderBrush>
<TextBlock Text="Drag and drop the file you want to view onto the screen!"
FontSize="18"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
DropFilesBehavisorExtension
public class DropFilesBehaviorExtension
{
public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached(
"IsEnabled", typeof(bool), typeof(DropFilesBehaviorExtension), new FrameworkPropertyMetadata(default(bool), OnPropChanged)
{
BindsTwoWayByDefault = false,
});
private static void OnPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!(d is FrameworkElement fe))
throw new InvalidOperationException();
if ((bool)e.NewValue)
{
fe.AllowDrop = true;
fe.Drop += OnDrop;
fe.PreviewDragOver += OnPreviewDragOver;
}
else
{
fe.AllowDrop = false;
fe.Drop -= OnDrop;
fe.PreviewDragOver -= OnPreviewDragOver;
}
}
private static void OnPreviewDragOver(object sender, DragEventArgs e)
{
// Border opacity will be raised on drag.
e.Effects = DragDropEffects.Move;
e.Handled = true;
}
private static void OnDrop(object sender, DragEventArgs e)
{
var dataContext = ((FrameworkElement)sender).DataContext;
if (!(dataContext is IFilesDropped filesDropped))
{
if (dataContext != null)
Trace.TraceError($"Binding error, '{dataContext.GetType().Name}' doesn't implement '{nameof(IFilesDropped)}'.");
return;
}
if (!e.Data.GetDataPresent(DataFormats.FileDrop))
return;
if (e.Data.GetData(DataFormats.FileDrop) is string[] files)
filesDropped.OnFilesDropped(files);
}
public static void SetIsEnabled(DependencyObject element, bool value)
{
element.SetValue(IsEnabledProperty, value);
}
public static bool GetIsEnabled(DependencyObject element)
{
return (bool)element.GetValue(IsEnabledProperty);
}
}
public interface IFilesDropped
{
void OnFilesDropped(string[] files);
}
DragLeave 功能解决了我的问题。一旦鼠标拖动离开所需区域,就会触发该功能。
fe.DragLeave += Fe_DragLeave;
private static void Fe_DragLeave(object sender, DragEventArgs e)
{
}