【问题标题】:MVVM listbox item contextmenuMVVM 列表框项上下文菜单
【发布时间】:2015-06-30 10:02:15
【问题描述】:

我在绑定列表框项目的上下文菜单时遇到问题。构建后我的上下文菜单不会显示任何项目。我搜索了很多,但没有任何积极的结果。上下文菜单仍然是空的。请问您知道我的问题的任何解决方案吗?

感谢您的帮助。

列表框:

<ListBox Name="uxTrendListBox" ItemsSource="{Binding SignalGroup.Trends}" SelectedIndex="0" DisplayMemberPath="TrendName" SelectedValue="{Binding SelectedTrend}" FontSize="14" FontWeight="Normal" BorderThickness="0" Foreground="white" DockPanel.Dock="Top" Margin="10" Background="#FF5B5A5A">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="ContextMenu">
                    <Setter.Value>
                        <ContextMenu ItemsSource="{Binding CommandList}">
                            <ContextMenu.ItemTemplate >
                                <DataTemplate>
                                    <MenuItem Header="{Binding Displayname}" Command="{Binding ContextMenuCommand}" />
                                </DataTemplate>
                            </ContextMenu.ItemTemplate>
                        </ContextMenu>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox >

命令列表:

private ObservableCollection<ContextMenuAction> commandList = new ObservableCollection<ContextMenuAction>();
        public ObservableCollection<ContextMenuAction> CommandList
        {
            get
            {
                return commandList;
            }
            set
            {
                Set(ref commandList, value);
            }
        }

ViewModel类的构造函数中填写的命令列表:

 CommandList.Add(new ContextMenuAction
        {
            Displayname = "Rename trend",
            ContextMenuCommand = TrendRenameCommand
        });
        CommandList.Add(new ContextMenuAction
        {
            Displayname = "Remove trend", 
            ContextMenuCommand = TrendRemoveCommand
        });

ContextMenuAction 类:

 public class ContextMenuAction : INotifyPropertyChanged
{
    private string displayName;

    public string Displayname
    {
        get { return displayName; }
        set
        {
            displayName = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Displayname"));
        }
    }

    private ICommand contextMenuCommand;

    public ICommand ContextMenuCommand
    {
        get { return contextMenuCommand; }
        set
        {
            contextMenuCommand = value;
            PropertyChanged(this, new PropertyChangedEventArgs("ContextMenuCommand"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

【问题讨论】:

    标签: c# wpf mvvm listbox


    【解决方案1】:
    1. 按 f5 启动调试应用程序。导航到页面并显示上下文菜单以在调试时重现错误

    2. 检查输出窗口是否存在绑定错误 (Menu-&gt;Debug-&gt;Windows-&gt;Output)。如果您在绑定时遇到错误,您应该在这里看到它

    3. 您在哪里定义了CommandList 属性?它应该在你的“趋势”类或任何你称之为的类中

    4. 当您在字段初始化程序中创建可观察集合的实例时,为什么您的 CommandList 属性上有公共设置器? 这可能不是问题,但通常您要么只从集合中添加/删除项目,要么您不修改集合但总是设置集合的新实例。你的班级两者都允许,而且有点味道

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      相关资源
      最近更新 更多