Pour info j'ai utilisé le blog de Thomas Lebrun pour mettre en place le Drag and Drop
Voici mon code côté source:
Le xaml:
1 2 3 4 5 6 7 8 9 10 11
| <Style x:Key="ColumnHeaderStyle" TargetType="{x:Type tk:DataGridColumnHeader}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding}" MouseMove="MouseMoveMethode"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style> |
Le code behind:
1 2 3 4 5 6 7 8 9 10
| private void MouseMoveMethode(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DragDropEffects effects;
DataObject obj = new DataObject();
obj.SetData(typeof(string), (sender as TextBlock).Text);
effects = DragDrop.DoDragDrop((sender as TextBlock), obj, DragDropEffects.Copy);
}
} |
Le code côté cible:
Le xaml:
<DockPanel Grid.Row="0" Background="Aqua" AllowDrop="True" DragOver="DragOverMethode" Drop="DropMethode">
Le code behind:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| private void DragOverMethode(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(string)))
{
e.Effects = DragDropEffects.Copy;
}
else
{
e.Effects = DragDropEffects.None;
}
}
private void DropMethode(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(string)))
{
e.Effects = DragDropEffects.Copy;
string uri = (string)e.Data.GetData(typeof(string));
(sender as DockPanel).Children.Add(new TextBlock { Text = uri });
}
else
{
e.Effects = DragDropEffects.None;
}
} |
Mais ça ne fonctionne pas, je passe bien dans le MouseMove mais jamais dans le Drop ni dans le DragOver.
Des idées?
Partager