mirror of
https://github.com/Tichau/FileConverter.git
synced 2026-11-19 09:01:32 +01:00
Auto scroll tree view when drag and droping an element.
This commit is contained in:
1 parent
0a46609274
commit
bb5e7ee7c2
4 files changed
+122
-2
No files matched your search
@@ -229,6 +229,7 @@
|
||||
<Compile Include="ViewModels\MainViewModel.cs" />
|
||||
<Compile Include="ViewModels\ViewModelLocator.cs" />
|
||||
<Compile Include="Views\BindableSelectedItemBehavior.cs" />
|
||||
<Compile Include="Views\DragDropExtension.cs" />
|
||||
<Compile Include="Views\HelpWindow.xaml.cs">
|
||||
<DependentUpon>HelpWindow.xaml</DependentUpon>
|
||||
</Compile>
|
||||
|
||||
@@ -196,4 +196,4 @@ namespace FileConverter.Views
|
||||
this.UpdateTreeViewItem((TreeViewItem)sender, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
// <copyright file="DragDropExtension.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
|
||||
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace FileConverter.Views
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides extended support for drag drop operation
|
||||
/// </summary>
|
||||
|
||||
public static class DragDropExtension
|
||||
{
|
||||
public static readonly DependencyProperty ScrollOnDragDropProperty = DependencyProperty.RegisterAttached("ScrollOnDragDrop", typeof(bool), typeof(DragDropExtension), new PropertyMetadata(false, HandleScrollOnDragDropChanged));
|
||||
|
||||
public static bool GetScrollOnDragDrop(DependencyObject element)
|
||||
{
|
||||
if (element == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(element));
|
||||
}
|
||||
|
||||
return (bool)element.GetValue(ScrollOnDragDropProperty);
|
||||
}
|
||||
|
||||
public static void SetScrollOnDragDrop(DependencyObject element, bool value)
|
||||
{
|
||||
if (element == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(element));
|
||||
}
|
||||
|
||||
element.SetValue(ScrollOnDragDropProperty, value);
|
||||
}
|
||||
|
||||
private static void HandleScrollOnDragDropChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
|
||||
{
|
||||
FrameworkElement container = dependencyObject as FrameworkElement;
|
||||
|
||||
if (dependencyObject == null)
|
||||
{
|
||||
Diagnostics.Debug.LogError("Invalid types!");
|
||||
return;
|
||||
}
|
||||
|
||||
Unsubscribe(container);
|
||||
|
||||
if (true.Equals(args.NewValue))
|
||||
{
|
||||
Subscribe(container);
|
||||
}
|
||||
}
|
||||
|
||||
private static void Subscribe(FrameworkElement container)
|
||||
{
|
||||
container.PreviewDragOver += OnContainerPreviewDragOver;
|
||||
}
|
||||
|
||||
private static void OnContainerPreviewDragOver(object sender, DragEventArgs args)
|
||||
{
|
||||
const double Tolerance = 60;
|
||||
const double Offset = 20;
|
||||
|
||||
FrameworkElement container = sender as FrameworkElement;
|
||||
if (container == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ScrollViewer scrollViewer = GetFirstVisualChild<ScrollViewer>(container);
|
||||
if (scrollViewer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
double verticalPos = args.GetPosition(container).Y;
|
||||
|
||||
if (verticalPos < Tolerance) // Top of visible list?
|
||||
{
|
||||
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - Offset); //Scroll up.
|
||||
}
|
||||
else if (verticalPos > container.ActualHeight - Tolerance) // Bottom of visible list?
|
||||
{
|
||||
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + Offset); //Scroll down.
|
||||
}
|
||||
}
|
||||
|
||||
private static void Unsubscribe(FrameworkElement container)
|
||||
{
|
||||
container.PreviewDragOver -= OnContainerPreviewDragOver;
|
||||
}
|
||||
|
||||
public static T GetFirstVisualChild<T>(DependencyObject dependencyObject) where T : DependencyObject
|
||||
{
|
||||
if (dependencyObject != null)
|
||||
{
|
||||
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
|
||||
{
|
||||
DependencyObject child = VisualTreeHelper.GetChild(dependencyObject, i);
|
||||
if (child is T visualChild)
|
||||
{
|
||||
return visualChild;
|
||||
}
|
||||
|
||||
T childItem = GetFirstVisualChild<T>(child);
|
||||
if (childItem != null)
|
||||
{
|
||||
return childItem;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,7 +85,8 @@
|
||||
|
||||
<TreeView Name="PresetTreeView" Grid.Row="0"
|
||||
ItemsSource="{Binding PresetsRootFolder.Children}"
|
||||
Width="{Binding ActualWidth, ElementName=PresetListGrid, Mode=OneWay}" AllowDrop="True">
|
||||
Width="{Binding ActualWidth, ElementName=PresetListGrid, Mode=OneWay}" AllowDrop="True"
|
||||
views:DragDropExtension.ScrollOnDragDrop="True">
|
||||
<TreeView.Resources>
|
||||
<HierarchicalDataTemplate DataType="{x:Type viewModels:PresetFolderNode}" ItemsSource="{Binding Children}">
|
||||
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
|
||||
|
||||
Reference in new issue
Block a user