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;
Related
I want to show just tree dataypes in my list view.(GeometryNode, ToolNode and StockNode). But my item source has 5 dataypes. How can I filter this datatypes?
My list view seems like that. And I want to remove or hide the items which are in top of the list view.
listview
<ListView.Resources>
<DataTemplate DataType="{x:Type treeview:MachineGeometryNodeViewItem}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding IconUri, Mode=OneWay}" Margin="0,0,5,0" Height="20" Width="20" />
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type treeview:MachineToolNodeViewItem}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding IconUri, Mode=OneWay}" Margin="0,0,5,0" Height="20" Width="20" />
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type treeview:MachineStockNodeViewItem}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding IconUri, Mode=OneWay}" Margin="0,0,5,0" Height="20" Width="20" />
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</ListView.Resources>
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>
I have a TreeView and want to get the selected item from it.
The Treeview itself is populated not manually, but from data in the code. Since it is populated this way, I'm not sure how to get information out of it.
Here is the XAML:
<TreeView Name="trvFamilies" HorizontalAlignment="Left" Margin="10,10,3,3" Width="340">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type self:Scene}" ItemsSource="{Binding Characters}">
<StackPanel Orientation="Horizontal">
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="New" Click="MenuItem_Click"></MenuItem>
<MenuItem Header="Remove" Click="MenuItem_Click_1"></MenuItem>
</ContextMenu>
</StackPanel.ContextMenu>
<Image Source="{StaticResource ImageSceneRegular}" Margin="0,0,5,0" Width="64" Height="64"/>
<TextBlock Text="{Binding Name}" />
<TextBlock Text=" [" Foreground="Blue" />
<TextBlock Text="{Binding Characters.Count}" Foreground="Blue" />
<TextBlock Text="]" Foreground="Blue" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type self:Character}">
<Border BorderThickness="1" Background="AliceBlue" CornerRadius="8,8,3,3">
<StackPanel Orientation="Horizontal" Margin="4" Background="White">
<Image Source="{Binding Img}" Margin="0,0,5,0" Width="64" Height="64" />
<TextBlock Text="{Binding Name}" />
<TextBlock Text=" (" Foreground="Green" />
<TextBlock Text="{Binding Age}" Foreground="Green" />
<TextBlock Text=" years)" Foreground="Green" />
</StackPanel>
</Border>
</DataTemplate>
</TreeView.Resources>
</TreeView>
It is populated by data from an ObservableCollection which is populated within the code and then assigned to the ItemSource of the treeview (and it works fine).
So let's say I had a TextBlock which sat outside the TreeView and wanted to populate it with information from the selected Character (that's the type which populates the drop down part of the TreeView), I don't know how to do that, be it using XAML only or code behind.
Any help on how to do this would be very much appreciated.
I have a TreeView in my XAML, each element of which is a StackPanel which allows each element to have an image and some text. They are orientated one atop the other as you can see here.
What I would like, and this may not be possible with a TreeView, is for the items (in this case 'FamilyMember') to appear horizontally rather than vertically. So, for example, you click the drop down for the family - you don't see them in a vertical list but in a horizontal one.
The XAML is as follows:
<TreeView Name="trvFamilies">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type self:Family}" ItemsSource="{Binding Members}">
<StackPanel Orientation="Horizontal">
<Image Source="{StaticResource ImageGroup}" Margin="0,0,5,0" />
<TextBlock Text="{Binding Name}" />
<TextBlock Text=" [" Foreground="Blue" />
<TextBlock Text="{Binding Members.Count}" Foreground="Blue" />
<TextBlock Text="]" Foreground="Blue" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type self:FamilyMember}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Text=" (" Foreground="Green" />
<TextBlock Text="{Binding Age}" Foreground="Green" />
<TextBlock Text=" years)" Foreground="Green" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
Any attempts made within the DataTemplate element only apply to each of the vertically aligned elements.
Maybe my efforts are on the wrong track if this is my goal. If so, if an alternative panel or control is better than I will gladly use it.
Thanks!
I have a Combobox whose ItemsSource is an ObservableCollection of int values.
My combobox itemtemplate consists on an image and a textblock which content is given by 2 converters.
How can I set this 2 bindings? The following code does not compile:
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding, Converter={StaticResource IntToImageConverter}}" Stretch="None" />
<TextBlock Text="{Binding, Converter={StaticResource IntToStringConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
You need to remove the , so:
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Converter={StaticResource IntToImageConverter}}" Stretch="None" />
<TextBlock Text="{Binding Converter={StaticResource IntToStringConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>