mvvm treeview不绑定viewmodel

时间:2012-06-01 23:14:18

标签: wpf mvvm treeview

我在树视图的实现中缺少什么? 这是看到的: enter image description here

查看:

<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"));
    }

和我的viewModel: 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]));
        }

TitleViewModel:

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;

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

1 个答案:

答案 0 :(得分:0)

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