0
votes

In my WPF application, I have added a TreeView control binding class data to TreeViewItems. I have added a context menu to the treeviewitems. The Handler of the contextMenu is not firing. Here is the XAML code for TreeView.

<TreeView ItemsSource="{Binding pads}" Width="190">
                    <TreeView.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}">
                            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                            <Setter Property="FontWeight" Value="Normal" />
                            <Setter Property="ContextMenu">
                                <Setter.Value>
                                    <ContextMenu>
                                        <MenuItem Header="Rename" Command="{Binding RenameCommand}"/>
                                    </ContextMenu>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="FontWeight" Value="Bold" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </TreeView.ItemContainerStyle>

                    <TreeView.ItemTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding Members}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Name}" />
                                <TextBlock Text=" [" Foreground="Blue" />
                                <TextBlock Text="{Binding Members.Count}" Foreground="Blue" />
                                <TextBlock Text="]" Foreground="Blue" />
                            </StackPanel>
                            <HierarchicalDataTemplate.ItemTemplate>
                                <DataTemplate DataType="{x:Type vm:PadInfo}">
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="["></TextBlock>
                                        <TextBlock Text="{Binding SlotID}" />
                                        <TextBlock Text="] ["></TextBlock>
                                        <TextBlock Text="{Binding WellID}" />
                                        <TextBlock Text="]"></TextBlock>
                                    </StackPanel>
                                </DataTemplate>
                            </HierarchicalDataTemplate.ItemTemplate>
                        </HierarchicalDataTemplate>
                    </TreeView.ItemTemplate>

                </TreeView>

These are the two classes that are binded to the TreeView.

    /// <summary>
/// Class to hold the Pads info for a tree
/// </summary>
public class Pad
{
    /// <summary>
    /// Default Constructor
    /// </summary>
    public Pad()
    {
        this.Members = new ObservableCollection<PadInfo>();
    }

    /// <summary>
    /// Name of the pad
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Members of the pad
    /// </summary>
    public ObservableCollection<PadInfo> Members { get; set; }
}

/// <summary>
/// Class to hold the well and slot IDs snapped to a pad
/// </summary>
public class PadInfo
{
    /// <summary>
    /// Slot ID
    /// </summary>
    public string SlotID { get; set; }

    /// <summary>
    /// Well ID
    /// </summary>
    public string WellID { get; set; }
}
}

This member is bind to the TreeView

public List<Pad> pads { get; set; }

When I right click on the menu items, the RenameCommand event is not firing. When I change the <TreeView.ItemTemplate> to <TreeViewItems> the handler is fired but TreeView is not populated with the binded data.

1

1 Answers

0
votes

Basically I am missing important code here about where is your class with the List, where is the command you bind to ? Furthermore you are using CommandBinding. This is not like a normal event handler. These are two things in WPF. I think you are missing the Property for your command in your DataContext - class.