✅ How to handle commands in TreeView?
According to the methodology by which I do this practical work, I need to visualize the territorial division of Kazakhstan using TreeView, using C# + WPF and the MVVM. What is the best way for me to make sure that I have 2 hierarchies (oblasts and cities), and that when I click on an object from the list, a command is triggered and displays information about this object to me (population, area, image, etc.)
14 Replies
I tried this
but it doesn't seem to work
Cities
here is ObservableCollection<Subject>
What is the propriate way to write a command for that? So when I click on a button (or any other thing that supports command implementation), object from that collection is taken and shown on the window (somewhere on Grid.Column="1"
)Is ShowSubject an
ICommand
?
If so, it should take something like an index or the object itself with CommandParameter
Notice the part
CommandParameter="{Binding}"
; personally I feel like this is non-intuitive and not very discoverable.![No description](https://cdn.answeroverflow.com/1185136649882058812/image.png)
![No description](https://cdn.answeroverflow.com/1185136650095952002/image.png)
@PepeGak see my answer above; if it works consider closing with /close.
Hi. I've tried the next thing:
I created an
ObservableCollection
(let's call it OC
) of TreeItemViewModel
(TIVM
) and inside the TIVM
class there is OC<SubjectModel>
of actual Subjects and string SubjectType {get;set;}
So now it looks like this (it's all in Russian but it works)
![No description](https://cdn.answeroverflow.com/1185529749800493257/image.png)
and redid commands thing. I found
RelayCommand
implementation and it works fine for me
But there is now another problem. Let's say I choose a subject (Astana city for example). How do I access it using TreeView?
Just iterate through all OC
's until the subject itself is found?You don't really access treeview directly
in my example, I have the command at the root level which contains the command to handle the treeviewitem command
and your treeviewitem command is bound to the SubjectModel
Best thing I could do is:
and XAML code for TreeView tag is
So when the item is selected, treeview is copied in my MainWindowViewModel
and in my viewmodel I'm working with Tree_View
I'm pretty sure that it violates the MVVM pattern but I hope my teachers accept that
in my command I have
Which sets the reference when the command is executed
I don't quite understand what does this command do
in your xaml, you have
When you click the treeview, a command will be sent
What you are attempting to do, is to take the subject of that command ( the command parameter ) and push it onto the reference for your main view model
how the command accomplishes that is covered in three areas;.
first...
Here the view model has the command, and a reference passed into the command for the viewmodel
MyCommand = new Command(this);
secondly we have
This is processed when the command is executed; the parameter is Item item
is a pattern match type check, if it's an Item ( or in your case a subject ) it creates a reference to the type as item. THen next, sets the mainviewmodel.Item to the typed item.
In your case, consider Item as Subject.
lastly in the xaml I have...
Here the command is bound to the root of the DataContext via the FindAncestor method; and the parameter, {Binding} would send your subject to the command.
so the reasons for Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.MyCommand}"
is that we only need one command to handle any Subject; but because you're in a HierachalDataTemplate, we have to find the Window.DataContext to set the proper command
@PepeGak so in practice, your button is the same as mine, it's executing a bound command, I'm just passing in the subject to the parameter.
Code like this can be confusing to follow; it eliminates having multiple types of commands, but at the expense of mixing a lot of code together.
You could have something like...
Then you have your hierarchal data template for your treeview.
btw... this part here is what I mean by confusing and difficult to follow...
In an effort to avoid a dedicated class we have a single type of command. It's not wrong, I just personally don't like it; it can be hard for someone to follow, especially in larger implementations.Hmm
it seems to fit my problem
i'll try it. Thanks!