3

I have created a treeview and now I want to get the name of the header as a string to use in the ViewModel. The command works but I can't get the name of the header to pass as a parameter in the method.

How do I get the name of the header each time I select a new new treeViewItem?

XAML

 <TreeView Name="EquipmentTreeView">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectedItemChanged">
                <i:InvokeCommandAction 
                     Command="{Binding SelectItemCommand}"
                     CommandParameter="{Binding SelectedItem, ElementName=EquipmentTreeView}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <TreeViewItem Header="Camera">
            <TreeViewItem Header="Digital Camera">                  
            </TreeViewItem >
            <TreeViewItem Header="Film Camera">
                <TreeViewItem Header="35mm Film Sound Camera"></TreeViewItem>
                <TreeViewItem Header="35mm Film MOS Camera"></TreeViewItem>
                <TreeViewItem Header="Film Magazines"></TreeViewItem>
            </TreeViewItem>
               ....

ViewModel (the parameter doesn't work)

 public class EquipmentManagerViewModel : NotifyUIBase
{

    public EquipmentManagerViewModel()
    {
        SelectItemCommand = new RelayCommand(() => GetItemHeader(SelectedItem));  
    }

    public RelayCommand SelectItemCommand { get; private set; }
    private void GetItemHeader(string selectedHeader)
    {
        MessageBox.Show(selectedHeader);
    }
}
Phil
  • 561
  • 2
  • 16
  • 29
  • Have you tried `CommandParameter="{Binding SelectedItem.Header, ElementName=EquipmentTreeView}"`? – Mike Eason Jul 10 '15 at 07:40
  • Actually the (SelectedItem) parameter in the relayCommand throws me an error of does not exist in current context – Phil Jul 10 '15 at 07:53
  • Forget the SelectedItem you must pass the parameter of you RelayCommand. The change that @Mike proposes is not necessary. – GreenEyedAndy Jul 10 '15 at 09:33
  • @GreenEyedAndy Mike's suggestion is a good one, coupled with your answer. That way you don't have to handle a TreeViewItem in the ViewModel (controls don't really belong in a ViewModel) – almulo Jul 10 '15 at 09:37
  • @almulo you are absolutely right - I will modify my answer. – GreenEyedAndy Jul 10 '15 at 09:58

1 Answers1

2

I don't know the implementation of your RelayCommand, but you must pass the commandParameter of the RelayCommand as Parameter of your GetItemHeader-Method. You are passing a SelectedItem which is not defined. Without any changes in you xaml do the following:

public EquipmentManagerViewModel()
{
    SelectItemCommand = new RelayCommand(tvi => GetItemHeader(((TreeViewItem)tvi).Header.ToString()));
}

But then I would rename the Method cause it is not doing what expected. It doesn't give you the ItemHeader! You extract the Header and give it to the Methode, the Method is displaying a MessageBox with a text as parameter.

Edit

The comment of @almulo leads me to the following changes:

In Xaml as @Mike proposes:

<i:InvokeCommandAction 
    Command="{Binding SelectItemCommand}"
    CommandParameter="{Binding SelectedItem.Header, ElementName=EquipmentTreeView}"/>

and in the ViewModel:

public EquipmentManagerViewModel()
{
    SelectItemCommand = new RelayCommand<String>(obj => GetItemHeader(obj.ToString()));
}
public RelayCommand<String> SelectItemCommand { get; private set; }
private void GetItemHeader(string selectedHeader)
{
    MessageBox.Show(selectedHeader);
}

all based on the fact that your RelayCommand can handle the CommandParameter.

Phil
  • 561
  • 2
  • 16
  • 29
GreenEyedAndy
  • 1,485
  • 1
  • 14
  • 31
  • I would modify this code so the Command receives the header string directly, instead of having to handle a TreeViewItem in the ViewModel. The OP would just have to change its CommandParameter Binding to `SelectedItem.Header` for that to work. – almulo Jul 10 '15 at 09:36
  • I just blind copy pasted your suggestion and it throws me an error saying Delegate system.action does not take 1 argument. I'm very green in working with c#. Any suggestions about how to fix that? – Phil Jul 10 '15 at 09:43
  • It depends on your implementation of RelayCommand. Try this one [link](https://gist.github.com/schuster-rainer/2648922) – GreenEyedAndy Jul 10 '15 at 09:52
  • @Phil - do you have further problems with that? Post your implementation of RelayCommand, we can check what's wrong. Otherwise it would be nice if you mark the answer. – GreenEyedAndy Jul 10 '15 at 11:27