0

So here I am again, with a probably supidly simple question for you, but it drives me crazy and I can't seem to find a solution to what I'm planning to do.

I'm generating a TreeView through a recursive DataTable

Datatable dtStorage;

ds.Tables.Add(dtStorage);
//add a relationship
ds.Relations.Add("rsParentChild", ds.Tables["Storagedata"].Columns["ID"],
ds.Tables["Storagedata"].Columns["CONTENTOF"]);

_rootNodes = ds.Tables["Storagedata"].DefaultView;
_rootNodes.RowFilter = "CONTENTOF IS NULL";

treeView.ItemsSource = _rootNodes;

Here is the XAML of the TreeView:

<TreeView  ContextMenuOpening="TextBlock_ContextMenuOpening" ItemsSource="{Binding RootNodes}" x:Name="treeView" SelectedItemChanged="treeView_SelectedItemChanged" BorderBrush="#FFCACACA" VerticalAlignment="Stretch" FontFamily="Courier New" Margin="0,0,-0.4,-0.2" 
         VirtualizingStackPanel.IsVirtualizing="True"
         VirtualizingStackPanel.VirtualizationMode="Recycling">
    <TreeView.Resources>
         <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
         <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Red" />
         <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="Transparent" />
         <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="Red" />
    </TreeView.Resources>

    <TreeView.ItemTemplate >
         <HierarchicalDataTemplate ItemsSource="{Binding rsParentChild}" >
              <StackPanel Tag="{Binding LABEL}" Orientation="Horizontal" ToolTip="{Binding ADDITIONALINFO}" Margin="0,2,0,0" >
                    <Image x:Name="TheImage" Tag="{Binding TYPE}"  Margin="0,0,2,0" Width="20" Height="20">
                         <!--Loaded="Image_Loaded"-->
                         <Image.ToolTip>
                              <TextBlock Text="{Binding ID, StringFormat=ID:{0}}" />
                         </Image.ToolTip>
                    </Image>
                    <TextBlock Text="{Binding LABEL}" ContextMenuOpening="TextBlock_ContextMenuOpening" VerticalAlignment="Center" Padding="2,0,0,0" ToolTip="{Binding ADDITIONALINFO}" >
                    </TextBlock>
              </StackPanel>
         </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

So far so good. My Treeview is creates as expected and I get a nice Treeview.
But I'd like to get a Drag & Drop function going.
My Problem is, and that's why almost any Tutorial I searched didn't work for me, I don't have TreeViewItems. For me there are DataRowViews.

Is it still possible to add a Simple Drag and Drop funcionallity?
I want to move ID-1 to ID-2, where ID-1 is the selected value and ID-2 is the target DataRowView.

Any help would be appretiated. Tipps, hints, solutions or critique.

Thanks in advance

Sunrunner
  • 15
  • 9
  • Do you have a TreeViewItem in your DragnDrop handler? If so, you can get your DataRowViews through (TreeViewItem.DataContext as DataRowView) See also https://stackoverflow.com/questions/1026179/drag-drop-in-treeview – Rekshino May 22 '17 at 16:28
  • Thanks, but unfortionally this article is about dragging a file INTO the Treeview. What I'm trying to do is having a functionallity that I can move something from A to B with Drag & Drop. As I said, I currently have no working Drag/Drop functions, because they all need a (TreeViewItem) to work and I can't provide that in my code. – Sunrunner May 23 '17 at 06:57
  • You have it definitely. Set the Style for TreeViewItem and trigger for mouse events and you will get your TreeViewItem with DataRowView in DataContext. See also https://stackoverflow.com/questions/639884/highlight-treeview-item-being-dragged-over – Rekshino May 23 '17 at 08:01

1 Answers1

0

So, here I am, feeling really dumb! A friend of me just said "why don't you use the stackpanel instead?!" And so I did....

private void sp_mouseDown(object sender, MouseButtonEventArgs e) {
 e.GetPosition(null);
 _sp = sender as StackPanel;
 drv = (DataRowView)_sp.DataContext;
 drv["ID"].ToString();
}

That and some more functions regarding the StackPanel did the trick. Nevertheless, your Comments were really helpfull and I'll definitly will look into them again!

Sunrunner
  • 15
  • 9