MVVM列表框项上下文菜单

时间:2015-06-30 10:02:16

标签: c# wpf mvvm listbox

我遇到了listbox item的绑定contextmenu的问题。构建后,我的contextmenu将不会显示任何项目。我搜索了很多但没有任何积极的结果。 Contextmenu仍然是空的。你知道我的问题的任何解决方案吗?

感谢您的帮助。

列表框:

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

CommandList:

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

2 个答案:

答案 0 :(得分:0)

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

  2. 检查输出窗口是否存在绑定错误(Menu->Debug->Windows->Output)。如果你有绑定错误,你应该在这里看到

  3. 您在哪里定义了CommandList属性?应该是你的'趋势&#39;上课或其他任何你称之为

  4. 为什么在字段初始值设定项中创建observable集合的实例时,CommandList属性上有公共setter? 这可能不是问题,但通常你只是从集合中添加/删除项目,或者你不修改集合但总是设置集合的新实例。你的班级允许两者,并且闻起来有点气味

答案 1 :(得分:0)

谢谢,将CommandList移动​​到我的Trend模型解决它!