如何将对象正确绑定到树视图

问题描述 投票:0回答:1

我一直在教自己C#,一直想编写一个使用树视图的WPF应用程序。我花了一点时间来弄清楚如何获取数据绑定以使用它。

有人可以告诉我我在哪里错吗?在这一点上,我只是想得到一些东西,任何东西都可以在树中显示。我发现的显示如何使用treeview的资源对我来说很混乱。

提前感谢您的时间

Cars.cs

using System;
using System.Collections.Generic;

namespace TreeViewTest
{
    public class Cars
    {
        public List<Car> CarCollection { get; set; }

    }

    public class Car
    {
        public String Make { get; set; }
        public String Model { get; set; }
        public int Year { get; set; }
        public List<String> Features { get; set; }

    }
}

MainWindowViewModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace TreeViewTest
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private List<Car> _Cars;

        public List<Car> Cars
        {
            get { return _Cars; }
            set
            {

                if (_Cars == value)
                {
                    return;
                }   
                _Cars = value;
                NotifyPropertyChanged();
            }
        }

        public Car _Car;
        public Car Car
        {
            get { return _Car; }
            set
            {
                if (_Car == value)
                {
                    return;
                }
                _Car = value;
                NotifyPropertyChanged();

            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows;

namespace TreeViewTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        MainWindowViewModel _MainWindowContext = new MainWindowViewModel();

        public MainWindow()
        {


            DataContext = _MainWindowContext;
            List<String> basicFeatures = new List<string>() { "Air Conditioning", "AM/FM Radio"};
            Car Rav4 = new Car() { Make = "Toyota", Model = "Rav4", Year = 2001, Features = basicFeatures};
            Car ModelX = new Car() { Make = "Tesla", Model = "Model X", Year = 2020, Features = basicFeatures};
            List<Car> theseCars = new List<Car>() { Rav4, ModelX };
            _MainWindowContext.Car = Rav4;
            _MainWindowContext.Cars = theseCars;

            InitializeComponent();
        }


    }
}

MainWindow.xaml

<Window x:Class="TreeViewTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TreeViewTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Grid.Column="0">    
            <TreeView Grid.Row="0" Grid.Column="0" Height="150">
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate ItemsSource="{Binding CarCollection}" DataType="{x:Type local:Cars}">
                        <StackPanel Orientation="Horizontal">
                            <TreeViewItem Header="{Binding Make}"/>
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>

        </StackPanel>

    </Grid>
</Window>
c# wpf data-binding treeview
1个回答
0
投票

添加

ItemsSource="{Binding Cars}"

到TreeView

© www.soinside.com 2019 - 2024. All rights reserved.