0
因此,我再次爲你提供一個可能很簡單的問題,但它使我發瘋,我似乎無法找到解決方案,我打算做。C#WPF拖放樹視圖元素
我通過遞歸數據表
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。
任何幫助將是appartiated。提示,提示,解決方案或批評。
在此先感謝
你的DragnDrop處理程序中是否有TreeViewItem?如果是這樣,你可以通過(TreeViewItem.DataContext作爲DataRowView)來獲取你的DataRowViews。另請參見https://stackoverflow.com/questions/1026179/drag-drop-in-treeview – Rekshino
謝謝,但不幸的是,這篇文章是關於拖拽文件INTO Treeview。我試圖做的是具有功能性,我可以通過拖放將某些內容從A移動到B.正如我所說,我目前沒有工作的拖放功能,因爲他們都需要一個(TreeViewItem)工作,我不能在我的代碼中提供。 – Sunrunner
你有它絕對。爲TreeViewItem設置樣式並觸發鼠標事件,然後在DataContext中使用DataRowView獲取TreeViewItem。另見https://stackoverflow.com/questions/639884/highlight-treeview-item-being-dragged-over – Rekshino