I have a TreeView and ListBox on the view. How can I change the data in the ListBox depending on what is selected in the TreeView? I found the SelectedItemChanged event at TreeView but I don’t understand how to use it with snapping. In fact, I just need a function call in which I will write something like

RegValues = SelectedKey.GetContent(); 

slice view.xaml

 <TreeView Name="tree" ItemsSource="{Binding TreeViewData}" SelectedValuePath="{Binding SelectedKey}" > <TreeView.ItemTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Path=ChildGroup}"> <TextBlock Text="{Binding name}"/> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView> 

a piece of viewmodel.cs

 public List<String> RegValues { get { int i = 0; return (List<String>)GetValue(RegValuesProperty); } set { SetValue(RegValuesProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty RegValuesProperty = DependencyProperty.Register("RegValues", typeof(List<String>), typeof(Level2PageViewModel)); public RegModel SelectedKey { get { return (RegModel)GetValue(SelectedKeyProperty); } set {SetValue(SelectedKeyProperty, value);} } // Using a DependencyProperty as the backing store for SelectedValue. This enables animation, styling, binding, etc... public static readonly DependencyProperty SelectedKeyProperty = DependencyProperty.Register("SelectedKey", typeof(RegModel), typeof(Level2PageViewModel)); public List<RegModel> TreeViewData { get { return (List<RegModel>)GetValue(TreeViewDataProperty); } set { SetValue(TreeViewDataProperty, value); } } // Using a DependencyProperty as the backing store for TreeViewData. This enables animation, styling, binding, etc... public static readonly DependencyProperty TreeViewDataProperty = DependencyProperty.Register("TreeViewData", typeof(List<RegModel>), typeof(Level2PageViewModel)); 
  • one
    For binding, you should not use events, but properties. In your case, create another property in the view model and bind it to the SelectedItem near TreeView - Bulson
  • one
    Yes, and who taught you to use the DependencyProperty view model? This is a game, to be honest. Use normal properties with the implementation of INotifyPropertyChanged . DependencyProperty` is only suitable for codebeehind, but not for viewmodel. - Bulson
  • @Bulson thanks. Nobody taught me, I saw the use of DependencyProperty in the view model in some kind of video on YouTube. If you have any good source on where and what to use, I will be very grateful. - Dark32167
  • one
    Tell us better what exactly you want to show. Tell us about your task at a higher level. So it will be clearer how to solve your problem correctly. - VladD
  • @VladD I need to display the windows registry, respectively, the window looks something like the regedit on the left of the treeview on the right of the datagrid. There is a class that contains a key, its subdirectories and its content implemented by another class. Therefore, by clicking on the treeview line, I want the content in the data grid to change. - Dark32167

1 answer 1

In fact, everything is simple.

Get a property like ObservableCollection<RegistryValue> , let it be called Values . At the beginning it contains an empty collection.

You have a SelectedKey property in your VM. You need to change the Values with the values ​​from the current key. To Values you bind your right pane, a DataGrid or just a ListView .

Now, how to subscribe to the SelectedKey change? This is easier to do, if your properties are defined through INotifyPropertyChanged , there you can simply call the desired function from the setter.

 private RegModel selectedKey public RegModel SelectedKey { get { return selectedKey; } set { if (Set(ref selectedKey, value)) UpdateValues(); } } 

( Set function can be found here ).

If you still want to use DependencyProperty in the VM (there is no idea for them, they have a place in the View), write this:

 public RegModel SelectedKey { get { return (RegModel)GetValue(SelectedKeyProperty); } set { SetValue(SelectedKeyProperty, value); } } public static readonly DependencyProperty SelectedKeyProperty = DependencyProperty.Register( "SelectedKey", typeof(RegModel), typeof(Level2PageViewModel), new FrameworkPropertyMetadata((o, args) => { var self = (Level2PageViewModel)o; self.UpdateValues(); })); 

It seems to be all.


PS: Your RegValues property is probably what I meant by Values .

  • thank. but why place a DependencyProperty in a view? I thought that in the view code nothing should be, in principle, Oo - Dark32167
  • one
    @ Dark32167: Well, look, here you have a control, for example, StackPanel . He is also in View, right? And it has a bunch of DependencyProperty . If you create your control, then you need some properties, you cannot create a new type of control at all without the code-behind. Here it makes sense to make them as DependencyProperty . - VladD