Does UWP TreeView reflect changes in items by dragging and droppping into it's associated ItemsSource from VM? For example, I associate TreeView with VM ObservableCollection list. When I add some items by executing command from VM that populate that collection with new items, they do show up in TreeView UI. However, if I drag and drop item inside TreeView, underlaying list does not change? What am I doing wrong? Should TreeView update and observable collection based on my actions?
Here is my VM:
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;
}
}
And here is my XAML for TreeView:
<localdata:MyTreeView
x:Name="treeview" Grid.Row="2" ItemsSource="{Binding storageFolders,Mode=OneWay}"
Style="{StaticResource TreeViewStyle1}"
/>