【发布时间】:2017-05-22 15:10:00
【问题描述】:
所以我又来了,有一个可能非常简单的问题要问你,但这让我发疯了,我似乎无法找到我打算做的事情的解决方案。
我正在通过递归 DataTable 生成 TreeView
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;
这是 TreeView 的 XAML:
<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>
到目前为止一切顺利。我的 Treeview 是按预期创建的,我得到了一个不错的 Treeview。
但我想要一个拖放功能。
我的问题是,这就是为什么我搜索的几乎所有教程都不适合我的原因,我没有 TreeViewItems。
对我来说,有 DataRowViews。
是否还可以添加简单的拖放功能?
我想将 ID-1 移动到 ID-2,其中 ID-1 是选定的值,ID-2 是目标 DataRowView。
任何帮助将不胜感激。提示、提示、解决方案或批评。
提前致谢
【问题讨论】:
-
您的 DragnDrop 处理程序中有 TreeViewItem 吗?如果是这样,您可以通过 (TreeViewItem.DataContext as DataRowView) 获取您的 DataRowViews 另见stackoverflow.com/questions/1026179/drag-drop-in-treeview
-
谢谢,但不幸的是,这篇文章是关于将文件拖入树视图的。我想要做的是有一个功能,我可以通过拖放将某些东西从 A 移动到 B。正如我所说,我目前没有可用的拖放功能,因为它们都需要一个 (TreeViewItem) 才能工作,而我无法在我的代码中提供它。
-
你肯定拥有它。设置 TreeViewItem 的样式并触发鼠标事件,您将在 DataContext 中获得带有 DataRowView 的 TreeViewItem。另见stackoverflow.com/questions/639884/…
标签: c# wpf drag-and-drop treeview