C# ObservableCollection 不会出现在 MVVM 的 TreeView 中

Kir*_*ill 0 c# wpf treeview mvvm visual-studio

所以我有一个集合Name,Code,Id和List与节点类型ServiceTypeDto这样的模式:

public class ServiceTypeDto
{
   public long Id
   public string Code
   public string Name
   public List<ServiceTypeDto> ChildrenList
}
Run Code Online (Sandbox Code Playgroud)

我有一个返回ServiceTypeDtos列表的方法,如下所示:

Dto列表

我有一个ChildernList暴露ServiceTypeDtos的。

这就是我尝试在ViewModel 中执行此操作的方式:

using System.Collections.Generic;
using System.Collections.ObjectModel;

using ServiceTypeService.Dto;
using ServiceTypeService.Interface;

using ShowServiceType.Interfaces;
using ShowServiceType.Utils;

namespace ShowServiceType.ViewModel
{
   class MainWindowViewModel : ViewModelBase
   {
      public string _name, _code;
      public long _id;
      public List<ServiceTypeDto> _childrenList = new List<ServiceTypeDto>();

      /// <summary>
      /// Create Services for work
      /// </summary>
      ILogService Log => Service.CreateLog();
      IExceptionHandler ExceptionHandler => Service.CreateExeptionHandler();
      IServiceType ServiceType => Service.CreateGetServiceType();


      public ObservableCollection<ServiceTypeDto> _servicesCollection;

      public MainWindowViewModel()
      {
         ServiceConfig.Initialization();
         var _services = ServiceType.GetServiceTypesTree();
         _servicesCollection = new ObservableCollection<ServiceTypeDto>();

         //This is convert to ObservableCollection my List<> =)
         foreach (var item in _services)
            _servicesCollection.Add(item);
      }

      public long ID
      {
         get => _id;
         set
         {
            _id = value;
            OnPropertyChanged("ID");
         }
      }
      public string Code
      {
         get => _code;
         set
         {
            _code = value;
            OnPropertyChanged("Code");
         }
      }
      public string Name
      {
         get => _name;
         set
         {
            _name = value;
            OnPropertyChanged("Name");
         }
      }
      public List<ServiceTypeDto> Children
      {
         get => _childrenList;
         set
         {
            _childrenList = value;
            OnPropertyChanged("Children");
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

我的ViewModelBase类型:

public class ViewModelBase : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;
    
   public void OnPropertyChanged( string propname ) => PropertyChanged?.Invoke(this , new PropertyChangedEventArgs(propname));
}
Run Code Online (Sandbox Code Playgroud)

我的主窗口的代码隐藏:

public MainWindow()
{
   InitializeComponent();
   ServiceConfig.Initialization();
   DataContext = new MainWindowViewModel();
}
Run Code Online (Sandbox Code Playgroud)

最后,这是视图的 XAML。

<Grid Grid.Row="1">
   <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Visible">
      <TreeView>
         <TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
               <StackPanel FlowDirection="LeftToRight" Orientation="Horizontal">
                  <TextBlock Text="{Binding ID}" />
                  <TextBlock Text="{Binding Code}" />
                  <TextBlock Text="{Binding Name}" />
               </StackPanel>
            </HierarchicalDataTemplate>
         </TreeView.ItemTemplate>
      </TreeView>
   </ScrollViewer>
</Grid>
Run Code Online (Sandbox Code Playgroud)

该ObservableCollection这么想的出现在TreeView。怎么了?

tha*_*guy 5

您的视图和视图模型中存在多个问题:

  • 将TreeView未绑定到任何东西,它绑定ItemSource到Children,否则它不会显示任何项目
  • 该ItemsSource的HierarchicalDataTemplate必须绑定到你中的子集ServiceTypeDto,这是ChildrenList不Children
  • 您没有填充Children集合(或其后备集合_childrenList ),因此它是空的。
  • 您将项目添加到_servicesCollection,但也未使用
  • 在ServiceTypeDto没有实现INotifyPropertyChanged,所以性质的变化将不会在用户界面中被反射
  • ChildrenListinServiceTypeDto不是 an ObservableCollection,因此添加或删除项目也不会反映在用户界面中

您应该考虑Children对您的属性和_children支持字段使用命名约定以提高代码的可读性,请在此处查看有关 C# 命名的参考。