UWP TreeView数据绑定并反映更改

时间:2019-06-03 13:02:52

标签: uwp drag-and-drop treeview viewmodel observablecollection

UWP TreeView是否通过从VM拖放到与其关联的ItemsSource中来反映项目的更改?例如,我将TreeView与VM ObservableCollection列表相关联。当我通过执行来自VM的命令来添加一些项目时,该命令用新项目填充了该集合,它们确实显示在TreeView UI中。但是,如果在TreeView中拖放项目,则底层列表不会更改?我究竟做错了什么? TreeView是否应该根据我的操作更新和观察到的集合?

这是我的虚拟机:

public class MySampleViewModel : MyBase 
{
    public ObservableCollection<FolderInfo> storageFolders { get; set; } = new ObservableCollection<FolderInfo>();

    public ICommand MyCommand { get; }

    public ICommand ChangeCommand { get; }

    public MySampleViewModel()
    {
        AddFilesFolders();
    }

    private void AddFilesFolders()
    {
        FolderInfo ff1 = new FolderInfo();
        ff1.FolderName = "Folder " + DateTime.Now.ToString();
        ff1.IsFolder = true;
        ff1.subFolders = new ObservableCollection<FolderInfo>();

        storageFolders.Add(ff1);

        FolderInfo ffkeko = new FolderInfo();
        ffkeko.FolderName = "subitem" + DateTime.Now.ToString();
        ffkeko.subFolders = new ObservableCollection<FolderInfo>();

        ff1.subFolders.Add(ffkeko);

        FolderInfo ff = new FolderInfo();
        ff.FolderName = "servername " + DateTime.Now.ToString();
        ff.subFolders = new ObservableCollection<FolderInfo>();

        storageFolders.Add(ff);
    }
}

public class MyBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class FolderInfo : MyBase
{
    private string _FolderName;
    public string FolderName
    {
        get { return _FolderName; }
        set
        {
            if (_FolderName != value)
            {
                _FolderName = value;
                OnPropertyChanged("FolderName");
            }
        }
    }

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

    public ObservableCollection<FolderInfo> subFolders { get; set; } = new ObservableCollection<FolderInfo>();

    public override string ToString()
    {
        return FolderName;
    }
}

这是我对TreeView的XAML:

<localdata:MyTreeView 
x:Name="treeview" Grid.Row="2" ItemsSource="{Binding storageFolders,Mode=OneWay}" 
Style="{StaticResource TreeViewStyle1}"
/>

1 个答案:

答案 0 :(得分:0)

问题是,发生更改时,您实际上是在向ObservableCollection<FolderInfo>属性分配新的subFolders实例,但是数据绑定绑定到了先前的实例,因此它不会观察到此更改。有两种方法可以解决此问题:

1)更改实例并引发PropertyChanged事件:

private ObservableCollection<FolderInfo> _subFolders = new ObservableCollection<FolderInfo>();
public ObservableCollection<FolderInfo> subFolders
{
   get => _subFolders;
   set
   {
      _subFolders = value;
      OnPropertyChanged();
   }
}

2)不用分配新实例,而是清除原始实例,然后添加新项目:

所以代替:

ff.subFolders = new ObservableCollection<FolderInfo>();
ff.subFolders.Add(something);

您将这样做:

ff.subFolders.Clear();
ff.subFolders.Add(something);