Im trying to get a TreeView working, that uses HierarchialDataTemplate to bind the Nodes to a ObservableCollection. The TreeViewItems are Grids with Checkboxes and Comboboxes in it. All that is kinda working, but I didn“t manage to get the Drag and Drop Feature working.
The code is based on TreeView, HierarchicalDataTemplate and recursive Data and http://www.codeproject.com/Articles/55168/Drag-and-Drop-Feature-in-WPF-TreeView-Control
<Window x:Class="TreeView_HierarchicalDataTemplate.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:TreeView_HierarchicalDataTemplate="clr-namespace:TreeView_HierarchicalDataTemplate"
Title="MainWindow" x:Name="mainWindow" Height="350" Width="525">
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVis" />
</Window.Resources>
<Grid>
<TreeView Name="TreeView_After" AllowDrop="True" DataContext="{Binding ElementName=mainWindow, Path=TreeModel}" ItemsSource="{Binding Items}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<EventSetter Event="TreeViewItem.DragOver" Handler="TreeView_After_DragOver"/>
<EventSetter Event="TreeViewItem.Drop" Handler="TreeView_After_Drop"/>
<EventSetter Event="TreeViewItem.MouseMove" Handler="TreeView_After_MouseMove"/>
<EventSetter Event="TreeViewItem.MouseDown" Handler="TreeView_After_MouseDown"/>
</Style>
</TreeView.ItemContainerStyle>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type TreeView_HierarchicalDataTemplate:NodeViewModel}" ItemsSource="{Binding Children}">
<Grid Background="LightBlue">
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Name}"></TextBlock>
<GridSplitter Visibility="{Binding Path=Show, Converter={StaticResource BoolToVis}}" Grid.Column="0" Width="5"/>
<CheckBox Visibility="{Binding Path=Show, Converter={StaticResource BoolToVis}}" Grid.Row="0" Grid.Column="1" Margin="5,5,0,0">activated</CheckBox>
<GridSplitter Visibility="{Binding Path=Show, Converter={StaticResource BoolToVis}}" Grid.Column="0" Width="5"/>
<StackPanel Visibility="{Binding Path=Show, Converter={StaticResource BoolToVis}}" Grid.Row="0" Grid.Column="2" Orientation="Horizontal">
<TextBlock Margin="5">Action:</TextBlock>
<ComboBox>
<TextBlock>Move To</TextBlock>
</ComboBox>
<TextBlock Margin="5">C:\\Videos\Folder\aVideo.mkv</TextBlock>
</StackPanel>
<GridSplitter Visibility="{Binding Path=Show, Converter={StaticResource BoolToVis}}" Grid.Column="1" Width="5"/>
<StackPanel Visibility="{Binding Path=Show, Converter={StaticResource BoolToVis}}" Grid.Row="0" Grid.Column="3" Orientation="Horizontal">
<TextBlock Margin="5">Duplicate of:</TextBlock>
<ComboBox>
<TextBlock>None</TextBlock>
</ComboBox>
</StackPanel>
</Grid>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
This is the Observable Collection
public class TreeViewModel
{
public ObservableCollection<NodeViewModel> Items { get; set; }
}
public class NodeViewModel : UIElement
{
public NodeViewModel()
{
Show = true;
}
public string Id { get; set; }
public string Name { get; set; }
public bool Show { get; set; }
public ObservableCollection<NodeViewModel> Children { get; set; }
}
But this Mouse down event is not working
private void TreeView_After_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
_lastMouseDown = e.GetPosition(TreeView_After);
}
}
Also I only got this typecast from element(UIElement) to container(NodeViewModel) to work if I derive NodeViewModel from UIElement. But if I derive NodeViewModel from UIElement the TreeView does not show anymore at all :(
private NodeViewModel GetNearestContainer(UIElement element)
{
// Walk up the element tree to the nearest tree view item.
NodeViewModel container = element as NodeViewModel;
while ((container == null) && (element != null))
{
element = VisualTreeHelper.GetParent(element) as UIElement;
container = element as NodeViewModel;
}
return container;
}