简体   繁体   中英

getting the name of header in treeviewItems

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);
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM