0

我在树视图的实现中缺少什么?这是所见: 在此处输入图像描述

看法:

<TreeView Style="{StaticResource ExpandAllStyle}" ItemsSource="{Binding Titles}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type vm:TitleViewModel}">
                    <Grid ShowGridLines="False">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition />
                            <ColumnDefinition />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>

                        <TextBlock Text="{Binding Path=DisplayedStartTime}" Margin="0,0,10,0" />
                        <TextBlock Grid.Column="1" Text="" />
                        <TextBlock Grid.Column="2" Text="{Binding Path=Text}" />
                    </Grid>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>

我的存储库:

public class TitleRepository : INotifyPropertyChanged
{
    #region Titles

    private ObservableCollectionEx<Title> _titles;

    public ObservableCollectionEx<Title> Titles
    {
        get { return _titles; }
        set 
        { 
            _titles = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Titles"));
        }
    }

    #endregion

    #region Current Title

    private Title _currentTitle;

    public Title CurrentTitle
    {
        get { return _currentTitle; }
        set { _currentTitle = value; }
    }

    #endregion

    public TitleRepository()
    {
        _titles = new ObservableCollectionEx<Title>();

        _titles.Add(new Title("This is a title test"));
    }

和我的视图模型:MainTreeViewModel:

    public class MainTreeViewModel : ViewModelBase
    {
        private readonly TitleRepository _titleRepository;

        private ObservableCollection<TitleViewModel> _titles;

        public ObservableCollection<TitleViewModel> Titles
        {
            get { return _titles; }
            set { _titles = value; }
        }

        public MainTreeViewModel()
        {
            _titleRepository = ((App)Application.Current).TitleRepository;

            _titleRepository.Titles.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Titles_CollectionChanged);

            _titles = new ObservableCollection<TitleViewModel>(
                (from t in _titleRepository.Titles
                 select new TitleViewModel(t)).ToList());
        }

        void Titles_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            _titles.Add(new TitleViewModel(_titleRepository.Titles[_titleRepository.Titles.Count - 1]));
        }

标题视图模型:

public class TitleViewModel : ViewModelBase
    {
        readonly Title _title;

        #region Displayed Start Time

        public string DisplayedStartTime
        {
            get { return _title.DisplayedStartTime; }
        }

        #endregion

        #region Text

        public string Text
        {
            get { return _title.Text; }
        }

        #endregion

        public TitleViewModel(Title title)
        {
            _title = title;


        }

我在 View.cs 中绑定视图模型:

        MainTreeViewModel vm = new MainTreeViewModel();
        this.DataContext = vm;

我希望看到我在存储库中填充的标题“这是一个磁贴测试”。它是一棵动态树。

4

1 回答 1

0

srry 发现了问题...... MainWindow 中的另一个绑定到 dataContext。MVVM 模式有效且有效。

于 2012-06-02T08:29:04.180 回答