Kkrulec
Kkrulec

Reputation: 23

How to show ListBox Items when TreeView Item is selected

So I have a task of building a mail application for school. I am having a problem with one of the parts.

I have a TreeView and a ListBox. TreeView has few items in it (Inbox, trash, draft). Now what I am trying to do, is that when I select and TreeView item certain ListBox Items will appear in the ListBox. (purpose of the ListBox is to show the mails in that "folder").

I have been looking into this, and there are some suggestions with ListAray and DataBinding, but I am very new and have no idea how to implement any of those.

What I have at this poit is:

<TreeView Grid.Row="2" Grid.ColumnSpan="1" VerticalAlignment="Stretch" HorizontalAlignment="Left" Margin="10,10,0,10"  Name="treeView1" Width="100" FontSize="14" SelectedItemChanged="treeView1_SelectedItemChanged">
    <TreeViewItem Header="Prejeto" IsSelected="True">
        <TreeViewItem Header="Prebrano" />
        <TreeViewItem Header="Neprebrano" />
    </TreeViewItem>

    <TreeViewItem Header="Poslano" />
    <TreeViewItem Header="Osnutki" />
    <TreeViewItem Header="Izbrisano" />
    <TreeViewItem Header="Nezaželeno" />
    <TreeViewItem />
</TreeView>

XAML ListBox:

<ListBox Name="seznamSporocil" Grid.Column="1" Grid.Row="2"  HorizontalAlignment="Left" Margin="10,10,0,10"  VerticalAlignment="Stretch" Width="100" FontWeight="Bold" FontFamily="Arial" MouseDoubleClick="seznamSporocil_MouseDoubleClick" />

SelectedItemChanged:

private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{

}

Upvotes: 1

Views: 2448

Answers (1)

Dennis
Dennis

Reputation: 37770

When working with WPF, data binding is your best friend. Just bind ItemsSource of list box with some collection property of tree view's selected item.

Update.

Here's the complete sample (just create WPF Application project). Model:

public class MailFolder
{
    public string Name { get; set; }

    public ObservableCollection<MailItem> Items
    {
        get
        {
            return items ?? (items = new ObservableCollection<MailItem>());
        }
    }
    private ObservableCollection<MailItem> items;

    public ObservableCollection<MailFolder> SubFolders
    {
        get
        {
            return subFolders ?? (subFolders = new ObservableCollection<MailFolder>());
        }
    }
    private ObservableCollection<MailFolder> subFolders;
}

public class MailItem
{
    public string Subject { get; set; }
}

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TreeView x:Name="MailTreeView" ItemsSource="{Binding}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local:MailFolder}" ItemsSource="{Binding SubFolders}">
                    <TextBlock Text="{Binding Name}"/>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>

        <ListBox Grid.Column="1" ItemsSource="{Binding Path=SelectedItem.Items, ElementName=MailTreeView}">
            <ListBox.ItemTemplate>
                <DataTemplate DataType="{x:Type local:MailItem}">
                    <TextBlock Text="{Binding Subject}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

And this is data context setup:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new[] 
        {
            new MailFolder
            {
                Name = "Prejeto",
                SubFolders = 
                {
                    new MailFolder
                    {
                        Name = "Prebrano",
                        Items = 
                        {
                            new MailItem { Subject = "A" },
                            new MailItem { Subject = "B" },
                            new MailItem { Subject = "C" },
                        }
                    },
                    new MailFolder
                    {
                        Name = "Neprebrano",
                        Items = 
                        {
                            new MailItem { Subject = "D" },
                            new MailItem { Subject = "E" },
                        }
                    },
                },
                Items = 
                {
                    new MailItem { Subject = "M" },
                    new MailItem { Subject = "N" },
                }
            },
            new MailFolder
            {
                Name = "Poslano",
                Items = 
                {
                    new MailItem { Subject = "F" },
                    new MailItem { Subject = "G" },
                }
            },
            new MailFolder
            {
                Name = "Osnutki",
                Items = 
                {
                    new MailItem { Subject = "H" },
                }
            },
            new MailFolder
            {
                Name = "Izbrisano",
                Items = 
                {
                    new MailItem { Subject = "I" },
                    new MailItem { Subject = "J" },
                    new MailItem { Subject = "K" },
                }
            },
            new MailFolder
            {
                Name = "Nezaželeno",
                Items = 
                {
                    new MailItem { Subject = "L" },
                }
            }
        };
    }
}

Note, that if you want to reflect changes, made to properties of your model classes, you need to implement INotifyPropertyChanged interface.

Upvotes: 1

Related Questions