如何绑定到WPF TreeView?

时间:2013-12-17 13:22:22

标签: c# wpf xaml treeview

我有一个带有Binding的TreeView,但在TreeView中只显示了第一级项目。我需要一个树视图=)我弄错了什么是错的。

这是我的代码:

MainWindow.xaml

<TreeView Margin="2.996,10,214,10" ItemsSource="{Binding Path=Urls}" Grid.Column="1">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate>
                <Grid>
                    <Rectangle Fill="{Binding Path=Color}" HorizontalAlignment="Left" Stroke="Black" VerticalAlignment="Top" Height="20" Width="20"/>
                    <TextBlock Text="{Binding Path=AbsoluteUrl}" Margin="25,0,0,0" />
                </Grid>
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=AbsoluteUrl}" />
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
</TreeView>

MainViewModel.cs (部分)

public ObservableCollection<Url> Urls { get; set; }

    public MainWindowViewModel()
    {
        Urls = new ObservableCollection<Url>();
    }

Url.cs

class Url : INotifyPropertyChanged
{
    public Url() { }

    public Url(string absoluteUrl, bool isBroken, string color)
    {
        AbsoluteUrl = absoluteUrl;
        IsBroken = isBroken;
        Color = color;
    }

    enum Status { Working, Broken };

    private ObservableCollection<Url> childUrlsValue = new ObservableCollection<Url>();
    public ObservableCollection<Url> ChildUrls
    {
        get
        {
            return childUrlsValue;
        }
        set
        {
            childUrlsValue = value;
        }
    }

    private string _absoluteUrl;
    public string AbsoluteUrl
    {
        get { return _absoluteUrl; }
        set
        {
            if (_absoluteUrl != value)
            {
                _absoluteUrl = value;
                OnPropertyChanged("AbsoluteUrl");
            }
        }
    }

    private bool _isBroken;
    public bool IsBroken
    {
        get { return _isBroken; }
        set
        {
            if (_isBroken != value)
            {
                _isBroken = value;
                OnPropertyChanged("IsBroken");
            }
        }
    }

    private string _color;
    public string Color
    {
        get { return _color; }
        set
        {
            if (_color != value)
            {
                _color = value;
                OnPropertyChanged("Color");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        { 
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

就是这个我正在向Urls添加项目:

Url DataGridTopic = new Url(startUrl.ToString(), true, "red");
                    DataGridTopic.ChildUrls.Add(
                        new Url(startUrl.ToString(), true, "red"));
                    DataGridTopic.ChildUrls.Add(
                        new Url(startUrl.ToString(), true, "red"));
                    DataGridTopic.ChildUrls.Add(
                        new Url(startUrl.ToString(), true, "red"));
                    Urls.Add(DataGridTopic);

1 个答案:

答案 0 :(得分:2)

您必须告诉 HierarchicalDataTemplate 使用其ItemsSource属性从哪里获取节点的子项。

在你的情况下:

<HierarchicalDataTemplate
     DataType="{x:Type my:Url}"
     ItemsSource="{Binding Path=ChildUrls}"
>
         ...
</HierarchicalDataTemplate>

还要注意 DataType 属性的用法,如果树的级别由不同的对象类型组成(目录和文件树就是这样的例子),这通常会成为必需。但是,我不确定这是否适用于您的方案。