AllowDrop=false not working WPF - wpf

I set AllowDrop only to the leaf node and AllowDrop=false to others but After setting AllowDrop = false it is still allowing Drop option.
<TreeView x:Name="tvParameters" >
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type loc:MyTreeViewItem}" ItemsSource="{Binding SubItems}">
<Grid >
<TextBlock Text="{Binding Path=FullName}" x:Name="tbFullParameter" Visibility="Collapsed"/>
<TextBlock Text="{Binding Path=Name}" AllowDrop="{Binding Path=AllowDrop}" x:Name="tbParameter" PreviewMouseLeftButtonDown="tbParameter_PreviewMouseLeftButtonDown" PreviewMouseMove="tbParameter_PreviewMouseMove"/>
<!--<TextBlock Text="{Binding Name}"/>-->
</Grid>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>

Related

WinUI 3 Height of SwipeItem

I would like to have TreeViewItems configured inside a SwipeControl; this works but the tree gets spaced really ugly, because I can't find a way to shrink the size of the SwipeItem; any idea where to look next?
Here is my Treeview-XAML
<TreeView ItemsSource="{x:Bind fileViewModel.collection}">
<TreeView.ItemTemplate>
<DataTemplate x:DataType="vm:vTreeItem">
<TreeViewItem ItemsSource="{x:Bind Children}">
<SwipeControl Height="25">
<SwipeControl.RightItems>
<SwipeItems Mode="Execute">
<SwipeItem x:Name="DeleteSwipeItem"
Background="Red"
Command="{Binding Source={StaticResource ViewModel}, Path=deleteCmd}"
CommandParameter="{x:Bind Name}"/>
</SwipeItems>
</SwipeControl.RightItems>
<StackPanel Orientation="Horizontal" Height="25">
<Image Width="20" Height="20" Source="{x:Bind Icon}"/>
<TextBlock Text="{x:Bind Name}" Margin="10,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"/>
</StackPanel>
</SwipeControl>
</TreeViewItem>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>

WPF - MVVM Treeview get selected item [duplicate]

This question already has answers here:
Data binding to SelectedItem in a WPF Treeview
(21 answers)
Closed 6 years ago.
‘SelectedItem’ property is read-only and cannot be set from markup. How i can get selected item?
<TreeView x:Name="TreeView1"
ItemsSource="{Binding Path=Champ}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type self:SimpleChampionship}" ItemsSource="{Binding Path=Tours}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type self:SimpleTourClient}" ItemsSource="{Binding Path=Matches}">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="Tour:" />
<TextBlock Text="{Binding Path=NameTour}" />
<TextBlock Text=" Matches [" />
<TextBlock Text="{Binding Path=Matches.Count}" />
<TextBlock Text="]" />
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type self:SimpleMatchClient}">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=Home}" />
<TextBlock Text="{Binding Path=HomeTeamGoals}" />
<TextBlock Text=" - " />
<TextBlock Text="{Binding Path=GuestTeamGoals}" />
<TextBlock Text="{Binding Path=Guest}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
example treeview
Need return selected SimpleMatchClient
Try this. It should work.
var treeViewItem = TreeView1.ItemContainerGenerator.ContainerFromItem(SelectedItem) as TreeViewItem;

TreeView with multiple levels

Of the following code
<TreeView Name="tree" ItemsSource="{Binding Path=AllNotes}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type m:Note}" ItemsSource="{Binding Path=ListIssuesType}">
<TextBlock Text="{Binding Path=SoftwareVersion}" Margin="2" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type m:IssueType}" ItemsSource="{Binding Path=IssueNames}">
<TextBlock Text="{Binding Path=IssueTypeName}" Margin="2" />
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type m:IssueType}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=IssueTypeName}" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
I get the error:
"Item has already been added. Key in dictionary: 'DataTemplateKey(ReleaseNotes_Window.Models.IssueType)' Key being added: 'DataTemplateKey(ReleaseNotes_Window.Models.IssueType)'"
When you put something into a ResourceDictionary it either needs an explicit x:Key or the key will be determined by the DictionaryKeyPropertyAttribute applied on the class.
For DataTemplate it is the following:
[DictionaryKeyProperty("DataTemplateKey")]
public class DataTemplate : FrameworkTemplate
This will depend on the DataType.
Because you have:
<HierarchicalDataTemplate DataType="{x:Type m:IssueType}" ItemsSource="{Binding Path=IssueNames}">
<TextBlock Text="{Binding Path=IssueTypeName}" Margin="2" />
</HierarchicalDataTemplate>
And
<DataTemplate DataType="{x:Type m:IssueType}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=IssueTypeName}" />
</StackPanel>
</DataTemplate>
Both with DataType="{x:Type m:IssueType}", that's why the program fails.
Use an additional x:Key for one of the DataTemplates and reference it as a StaticResource where you're planning to use it.

How to Binding ListBox selected item and TextBox?

I have listBox and i want to see the selected item ftom the listBox on some textBox.
(the Collection is a list of string)
I trying to write the code but this is not working.
<ListBox x:Name="Collection__" Grid.Row="0" ItemsSource="{Binding Collection}" />
<StackPanel Grid.Row="0" Grid.Column="1" Orientation="Vertical" Margin="5">
<DockPanel>
<TextBlock DockPanel.Dock="Left" Text="Collection name:"/>
<TextBox DockPanel.Dock="Left" Margin="5,0" Text="{Binding ElementName=Collection__, Path=SelectedItem}"/>
</DockPanel>
</StackPanel>
found it ...
<StackPanel>
<ListBox x:Name="ElementListBox" ItemsSource="{Binding Elements}"
IsSynchronizedWithCurrentItem="True">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Text="{Binding Elements/Name}"/>
</StackPanel>

WPF Treeview HierarchicalDataTemplate Drag and drop

I have a treeview in wpf that is built using the xaml below. It is a well structured data source, and I am having a lot of trouble dragging and dropping. I have tried several methods, all to no avail. Can anyone tell me what the standard procedure is for doing this type of thing?
<TreeView x:Name="_treeView" ItemsSource="{Binding}" Grid.Row="0" Grid.Column="0">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type Logic:Statement}"
ItemsSource="{Binding Path=PagedChildren}">
<TextBlock Text="{Binding StatementName}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type Logic:StatementPage}"
ItemsSource="{Binding Path=Children}">
<WrapPanel>
<TextBlock Text="Page: "/>
<TextBlock Text="{Binding PageIndex}"/>
</WrapPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type Logic:StatementFund}">
<Border HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="2" CornerRadius="25">
<WrapPanel Margin="30 0 30 0" Width="150" Height="150" >
<StackPanel>
<TextBlock Text="Fund"/>
<WrapPanel>
<TextBlock Text="Fund: "/>
<TextBlock Text="{Binding FundNumber}"/>
</WrapPanel>
<WrapPanel Margin="10 0 0 0">
<TextBlock Text="{Binding ColumnIndex}"/>
</WrapPanel>
</StackPanel>
</WrapPanel>
</Border>
</DataTemplate>
<DataTemplate DataType="{x:Type Logic:StatementPreviousCycle}">
<Border HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="2" CornerRadius="25">
<WrapPanel Margin="30 0 30 0" Width="150" Height="150" >
<StackPanel>
<TextBlock Text="Previous Cycle"/>
<WrapPanel>
<TextBlock Text="Fund: "/>
<TextBlock Text="{Binding FundNumber}"/>
</WrapPanel>
<WrapPanel Margin="10 0 0 0">
<TextBlock Text="{Binding ColumnIndex}"/>
</WrapPanel>
</StackPanel>
</WrapPanel>
</Border>
</DataTemplate>
</TreeView.Resources>
</TreeView>
i use the techniques on this site for a general drag and drop.
tree view can get messy, if you want to know which node you are preivewMouseDown'ing on, to then use as your drag item you can end up walking the visual tree. there is some code to do that here. another way is to subclass treeview, and treeviewitem, then you can override the preview mouse down on each tree view item, and tell your derived parent treeview about it, which could set the tree view item to be the selected item.

Resources