0

在我的 WPF 应用程序中,我已将 TreeView 控件绑定类数据添加到 TreeViewItems。我在树视图项中添加了一个上下文菜单。contextMenu 的处理程序未触发。这是 TreeView 的 XAML 代码。

<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>

这是绑定到 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; }
}
}

此成员绑定到 TreeView

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

当我右键单击菜单项时,不会触发 RenameCommand 事件。当我更改<TreeView.ItemTemplate><TreeViewItems>处理程序时,将触发但 TreeView 未填充绑定数据。

4

1 回答 1

0

基本上我在这里遗漏了重要的代码,关于你的类与列表在哪里,你绑定到的命令在哪里?此外,您正在使用 CommandBinding。这不像普通的事件处理程序。这是 WPF 中的两件事。我认为您在 DataContext - 类中缺少命令的属性。

于 2013-07-08T05:59:31.363 回答