简体   繁体   English

选择TreeView项时如何显示ListBox项

[英]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和一个ListBox TreeView has few items in it (Inbox, trash, draft). TreeView中几乎没有项目(收件箱,回收站,草稿)。 Now what I am trying to do, is that when I select and TreeView item certain ListBox Items will appear in the ListBox . 现在我想做的是,当我选择TreeView项时,某些ListBox项将出现在ListBox (purpose of the ListBox is to show the mails in that "folder"). ListBox目的是在该“文件夹”中显示邮件)。

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. 我一直在研究这个问题,并且ListArayDataBinding有一些建议,但是我很新,不知道如何实现这些建议。

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 : 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 : SelectedItemChanged

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

}

When working with WPF, data binding is your best friend. 使用WPF时, 数据绑定是您最好的朋友。 Just bind ItemsSource of list box with some collection property of tree view's selected item. 只需将列表框的ItemsSource与树视图的所选项目的某些集合属性绑定即可。

Update . 更新

Here's the complete sample (just create WPF Application project). 这是完整的示例(只需创建WPF应用程序项目)。 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: 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. 注意,如果要反映对模型类的属性所做的更改,则需要实现INotifyPropertyChanged接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM