2

I try get original value on WPF treeview.

Commonly, Treeview we get selected item' original value using

object Item = treeview1.SelectedItem;
MessageBox.Show(Item.ToString());

but, my attempts to get it using this method were unsuccessful.

If I try it, then I get a "WPFName+TreeItem" MessageBox

This is my code with WPF

C#

private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    object temp = treeView.SelectedItem;
    MessageBox.Show(temp.ToString());
}

private static IEnumerable<TreeItem> GetChannelTreeForTreeView(QueryRunner queryRunner)
{
    List<ChannelTreeItem> channelTree = queryRunner.Utils.GetChannelTree(false);

    foreach (ChannelTreeItem channelTreeItem in channelTree)
    {
        TreeItem treeViewItem = new TreeItem { Data = channelTreeItem.Channel };
        FillTreeViewItem(treeViewItem, channelTreeItem);
        yield return treeViewItem;
    }
}

private static void FillTreeViewItem(TreeItem treeViewItem, ChannelTreeItem channelTreeItem)
{
    foreach (ClientListEntry clientListEntry in channelTreeItem.Clients)
        if (clientListEntry.Nickname.Contains("serveradmin from") == false)
        {
            treeViewItem.Children.Add(new TreeItem { Data = clientListEntry });
        }

    foreach (ChannelTreeItem childChannelTreeItem in channelTreeItem.Children)
    {
        TreeItem childTreeViewItem = new TreeItem { Data = childChannelTreeItem.Channel };
        treeViewItem.Children.Add(childTreeViewItem);
        FillTreeViewItem(childTreeViewItem, childChannelTreeItem);
    }
}

public class TreeItem
{
    public object Data { get; set; }
    public List<TreeItem> Children { get; private set; }

    public TreeItem()
    {
        Children = new List<TreeItem>();
    }
}

WPF

<TreeView x:Name="treeView" HorizontalAlignment="Left" 
              Height="265" VerticalAlignment="Top" Width="353" 
              SelectedItemChanged="treeView_SelectedItemChanged"
              MouseUp="treeView_MouseUp">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type self:ViewItemWPF+TreeItem}" ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal">
                    <ContentPresenter Content="{Binding Data}"/>
                </StackPanel>
            </HierarchicalDataTemplate>
            <DataTemplate DataType="{x:Type HelperClasses:ChannelListEntry}">
                <StackPanel Orientation="Horizontal">
                    <Border Background="Green" Width="8" Height="12" BorderBrush="#00000000"></Border>
                    <TextBlock Text="{Binding Path=Name}"/>
                </StackPanel>
            </DataTemplate>
            <DataTemplate DataType="{x:Type HelperClasses:ClientListEntry}" >
                <StackPanel Orientation="Horizontal">
                    <Border Background="DarkBlue" Width="8" Height="12" BorderBrush="#00000000"></Border>
                    <TextBlock Text="{Binding Path=Nickname}" Foreground="blue" />
                </StackPanel>
            </DataTemplate>
        </TreeView.Resources>
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="True" />
            </Style>
        </TreeView.ItemContainerStyle>
    </TreeView>

Can I get original value using foreach or others methods?

mm8
  • 135,298
  • 10
  • 37
  • 59
  • `TreeViewItem.Selected` is a `RoutedEvent` that bubbles up your TreeView. RoutedEvents have an `OriginalSource` property that should be sufficient for this. If by *"Original Value"* you mean the Item that was used to generate the instance of the ItemTemplate inside the TreeViewItem, simply use the `DataContext` of your `TreeViewItem`. – Manfred Radlwimmer Feb 20 '17 at 09:57

1 Answers1

0

You could cast the SelectedItem property to a TreeItem and then cast its Data property to the appropriate type. You can then access any properties of your classes as usual.

It is unclear what type "Channel" is but the following sample code should give you the idea:

private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    TreeItem temp = treeView.SelectedItem as TreeItem;
    if (temp != null)
    {
        ClientListEntry clientListEntry = temp.Data as ClientListEntry;
        if (clientListEntry != null)
        {
            //Data is a ClientListEntry
            //...
            return;
        }

        Channel channel = temp.Data as ClientListEntry;
        if (channel != null)
        {
            //...
        }

    }
    MessageBox.Show(temp.ToString());
}
mm8
  • 135,298
  • 10
  • 37
  • 59