Skip to content

Commit 1edf976

Browse files
bsundsbopunker76
authored andcommitted
Added sample to demonstrate
1 parent eba1558 commit 1edf976

File tree

6 files changed

+231
-0
lines changed

6 files changed

+231
-0
lines changed

src/Directory.packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<PackageVersion Include="MahApps.Metro.IconPacks.FeatherIcons" Version="5.1.0" />
1919
<PackageVersion Include="MahApps.Metro.IconPacks.Material" Version="5.1.0" />
2020
<PackageVersion Include="MahApps.Metro.IconPacks.Octicons" Version="5.1.0" />
21+
<PackageVersion Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.37" />
2122
<PackageVersion Include="Faker.Net" Version="2.0.163" />
2223
<PackageVersion Include="Costura.Fody" Version="6.0.0" />
2324

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Showcase.WPF.DragDrop.Models;
2+
3+
using System.Windows;
4+
using System.Windows.Controls;
5+
using Microsoft.Xaml.Behaviors;
6+
using DragDrop = GongSolutions.Wpf.DragDrop.DragDrop;
7+
8+
public class ComboBoxDropBehavior : Behavior<ComboBox>
9+
{
10+
protected override void OnAttached()
11+
{
12+
base.OnAttached();
13+
AssociatedObject.Loaded += AssociatedObject_Loaded;
14+
}
15+
16+
protected override void OnDetaching()
17+
{
18+
base.OnDetaching();
19+
AssociatedObject.Loaded -= AssociatedObject_Loaded;
20+
}
21+
22+
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
23+
{
24+
if (AssociatedObject.Template.FindName("PART_EditableTextBox", AssociatedObject) is not TextBox textBox)
25+
{
26+
return;
27+
}
28+
29+
// Create and set the custom drop info builder
30+
var dropInfoBuilder = new ComboBoxDropInfoBuilder(AssociatedObject);
31+
DragDrop.SetDropInfoBuilder(textBox, dropInfoBuilder);
32+
33+
// Attach Gong DragDrop manually
34+
DragDrop.SetIsDropTarget(textBox, DragDrop.GetIsDropTarget(AssociatedObject));
35+
}
36+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace Showcase.WPF.DragDrop.Models;
2+
3+
using System.Windows;
4+
using System.Windows.Controls;
5+
using GongSolutions.Wpf.DragDrop;
6+
7+
public class ComboBoxDropHandler : IDropTarget
8+
{
9+
public void DragEnter(IDropInfo dropInfo)
10+
{
11+
// nothing here
12+
}
13+
14+
public void DragLeave(IDropInfo dropInfo)
15+
{
16+
// nothing here
17+
}
18+
19+
private bool IsAcceptedItem(IDragInfo data)
20+
{
21+
return data?.Data is ListBoxItem;
22+
}
23+
24+
public void DropHint(IDropHintInfo dropHintInfo)
25+
{
26+
if (!IsAcceptedItem(dropHintInfo.DragInfo))
27+
{
28+
if (dropHintInfo.DragInfo == null)
29+
{
30+
return;
31+
}
32+
33+
dropHintInfo.DropHintText = "Cannot drop here";
34+
dropHintInfo.DropTargetHintAdorner = DropTargetAdorners.Hint;
35+
dropHintInfo.DropTargetHintState = DropHintState.Error;
36+
return;
37+
}
38+
39+
dropHintInfo.DropHintText = "Drop here";
40+
dropHintInfo.DropTargetHintAdorner = DropTargetAdorners.Hint;
41+
}
42+
43+
public void DragOver(IDropInfo dropInfo)
44+
{
45+
if (!IsAcceptedItem(dropInfo.DragInfo))
46+
{
47+
if (dropInfo.DragInfo == null)
48+
{
49+
return;
50+
}
51+
52+
dropInfo.DropHintText = "Cannot drop here";
53+
dropInfo.DropTargetHintAdorner = DropTargetAdorners.Hint;
54+
dropInfo.DropTargetHintState = DropHintState.Error;
55+
return;
56+
}
57+
58+
dropInfo.DropHintText = "Let go!";
59+
dropInfo.DropTargetHintState = DropHintState.Active;
60+
dropInfo.DropTargetHintAdorner = DropTargetAdorners.Hint;
61+
dropInfo.Effects = DragDropEffects.Copy;
62+
dropInfo.EffectText = "drop";
63+
dropInfo.DestinationText = "Drop";
64+
}
65+
66+
public void Drop(IDropInfo dropInfo)
67+
{
68+
dropInfo.DropTargetAdorner = DropTargetAdorners.Hint;
69+
dropInfo.Effects = DragDropEffects.Copy;
70+
}
71+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace Showcase.WPF.DragDrop.Models;
2+
3+
using System.Windows;
4+
using System.Windows.Controls;
5+
using GongSolutions.Wpf.DragDrop;
6+
using GongSolutions.Wpf.DragDrop.Utilities;
7+
8+
/// <summary>
9+
/// Drop info builder for ComboBox to redirect drop information to the ComboBox itself instead
10+
/// of the internal TextBox.
11+
/// </summary>
12+
/// <param name="comboBox"></param>
13+
public class ComboBoxDropInfoBuilder(ComboBox comboBox) : IDropInfoBuilder
14+
{
15+
public IDropInfo CreateDropInfo(object sender, DragEventArgs e, IDragInfo dragInfo, EventType eventType)
16+
{
17+
if (!comboBox.IsEditable)
18+
{
19+
return null;
20+
}
21+
22+
// If the target is the TextBox inside ComboBox, we want to create
23+
// drop info as if the drop happened on the ComboBox itself
24+
if (e.Source is TextBox || (e.Source is UIElement element && element.GetVisualAncestor<TextBox>() != null))
25+
{
26+
e.Source = comboBox;
27+
e.Handled = true;
28+
return new DropInfo(comboBox, e, dragInfo, eventType);
29+
}
30+
31+
return null;
32+
}
33+
}

src/Showcase/Showcase.WPF.DragDrop.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25+
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" />
2526
<PackageReference Include="MahApps.Metro.IconPacks.FeatherIcons" />
2627
<PackageReference Include="MahApps.Metro.IconPacks.Material" />
2728
<PackageReference Include="MahApps.Metro.IconPacks.Octicons" />

src/Showcase/Views/Issues.xaml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
99
xmlns:models="clr-namespace:Showcase.WPF.DragDrop.Models"
1010
xmlns:viewModels="clr-namespace:Showcase.WPF.DragDrop.ViewModels"
11+
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
1112
d:DataContext="{d:DesignInstance viewModels:MainViewModel}"
1213
d:DesignHeight="400"
1314
d:DesignWidth="600"
@@ -1248,6 +1249,94 @@
12481249
</DockPanel>
12491250
</TabItem>
12501251

1252+
<TabItem Header="#519">
1253+
<TabItem.Resources>
1254+
<models:ComboBoxDropHandler x:Key="ComboBoxDropHandler"/>
1255+
<DataTemplate x:Key="DefaultDropHintTemplate" DataType="{x:Type dd:DropHintData}">
1256+
<Border x:Name="RootBorder"
1257+
Background="#008000"
1258+
BorderBrush="#008000"
1259+
BorderThickness="1"
1260+
CornerRadius="5">
1261+
<TextBlock HorizontalAlignment="Center"
1262+
VerticalAlignment="Center"
1263+
Foreground="White"
1264+
Text="{Binding HintText}"
1265+
TextWrapping="Wrap" />
1266+
</Border>
1267+
<DataTemplate.Triggers>
1268+
<DataTrigger Binding="{Binding Path=HintState}" Value="Active">
1269+
<Setter TargetName="RootBorder" Property="Background" Value="#263B80" />
1270+
</DataTrigger>
1271+
<DataTrigger Binding="{Binding Path=HintState}" Value="Error">
1272+
<Setter TargetName="RootBorder" Property="Background" Value="#8B0000" />
1273+
</DataTrigger>
1274+
</DataTemplate.Triggers>
1275+
</DataTemplate>
1276+
</TabItem.Resources>
1277+
<DockPanel LastChildFill="True">
1278+
<Grid DockPanel.Dock="Top">
1279+
<TextBlock Style="{StaticResource SampleHeaderTextBlockStyle}" Text="Drop hint not available for TextBox part of ComboBox when IsEditable=true." />
1280+
<Button CommandParameter="519"
1281+
Style="{StaticResource GitHubPullRequestButtonStyle}"
1282+
ToolTip="Open #519 on Github" />
1283+
</Grid>
1284+
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
1285+
<StackPanel>
1286+
1287+
<UniformGrid Columns="3">
1288+
<TextBlock Text="#1 allows drop only on glyph" TextWrapping="Wrap"/>
1289+
<TextBlock Text="#2 allows on whole (behavior)" TextWrapping="Wrap"/>
1290+
<TextBlock Text="#3 allows on whole " TextWrapping="Wrap"/>
1291+
<ComboBox IsEditable="True"
1292+
Text="IsEditable"
1293+
Margin="2"
1294+
Width="150"
1295+
dd:DragDrop.IsDropTarget="True"
1296+
dd:DragDrop.DropHandler="{StaticResource ComboBoxDropHandler}"
1297+
dd:DragDrop.UseDropTargetHint="True"
1298+
dd:DragDrop.DropHintDataTemplate="{StaticResource DefaultDropHintTemplate}"
1299+
/>
1300+
<ComboBox IsEditable="True"
1301+
Text="IsEditable Behavior"
1302+
Margin="2"
1303+
Width="150"
1304+
dd:DragDrop.IsDropTarget="True"
1305+
dd:DragDrop.DropHandler="{StaticResource ComboBoxDropHandler}"
1306+
dd:DragDrop.UseDropTargetHint="True"
1307+
dd:DragDrop.DropHintDataTemplate="{StaticResource DefaultDropHintTemplate}">
1308+
<b:Interaction.Behaviors>
1309+
<models:ComboBoxDropBehavior />
1310+
</b:Interaction.Behaviors>
1311+
</ComboBox>
1312+
<ComboBox IsEditable="False"
1313+
Text="IsEditable False"
1314+
Margin="2"
1315+
Width="150"
1316+
dd:DragDrop.IsDropTarget="True"
1317+
dd:DragDrop.DropHandler="{StaticResource ComboBoxDropHandler}"
1318+
dd:DragDrop.UseDropTargetHint="True"
1319+
dd:DragDrop.DropHintDataTemplate="{StaticResource DefaultDropHintTemplate}"
1320+
/>
1321+
</UniformGrid>
1322+
<Grid>
1323+
<Grid.ColumnDefinitions>
1324+
<ColumnDefinition Width="Auto" />
1325+
</Grid.ColumnDefinitions>
1326+
1327+
<ListBox MinWidth="200"
1328+
dd:DragDrop.IsDragSource="True">
1329+
<ListBoxItem Content="Item 1" />
1330+
<ListBoxItem Content="Item 2" />
1331+
<ListBoxItem Content="Item 3" />
1332+
<ListBoxItem Content="Item 4" />
1333+
<ListBoxItem Content="Item 5" />
1334+
</ListBox>
1335+
</Grid>
1336+
</StackPanel>
1337+
</ScrollViewer>
1338+
</DockPanel>
1339+
</TabItem>
12511340
</TabControl>
12521341

12531342
</Grid>

0 commit comments

Comments
 (0)