Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVVM列表框项目上下文菜单_C#_Wpf_Mvvm_Listbox - Fatal编程技术网

C# MVVM列表框项目上下文菜单

C# MVVM列表框项目上下文菜单,c#,wpf,mvvm,listbox,C#,Wpf,Mvvm,Listbox,我在绑定listbox项的contextmenu时遇到问题。生成后,我的上下文菜单将不显示任何项目。我搜索了很多,但没有任何积极的结果。Contextmenu仍为空。你知道我的问题有什么解决办法吗 谢谢你的帮助 列表框: <ListBox Name="uxTrendListBox" ItemsSource="{Binding SignalGroup.Trends}" SelectedIndex="0" DisplayMemberPath="TrendName" SelectedValue=

我在绑定listbox项的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 >
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 { };
}
  • 按f5启动调试应用程序。导航到页面并显示上下文菜单以在调试时再现错误

  • 检查输出窗口是否存在绑定错误(
    菜单->调试->窗口->输出
    )。如果绑定中有错误,应该在这里看到

  • 您在哪里定义了
    CommandList
    属性?它应该出现在你的“趋势”课上,或者你管它叫什么

  • 在字段初始值设定项中创建可观察集合的实例时,为什么在
    CommandList
    属性上有公共setter? 这可能不会引起问题,但通常您要么只是从集合中添加/删除项,要么不修改集合,而是始终设置集合的新实例。你们班两个都允许,这有点异味

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

  • 检查输出窗口是否存在绑定错误(
    菜单->调试->窗口->输出
    )。如果绑定中有错误,应该在这里看到

  • 您在哪里定义了
    CommandList
    属性?它应该出现在你的“趋势”课上,或者你管它叫什么

  • 在字段初始值设定项中创建可观察集合的实例时,为什么在
    CommandList
    属性上有公共setter? 这可能不会引起问题,但通常您要么只是从集合中添加/删除项,要么不修改集合,而是始终设置集合的新实例。你们班两个都允许,这有点异味

  •  CommandList.Add(new ContextMenuAction
            {
                Displayname = "Rename trend",
                ContextMenuCommand = TrendRenameCommand
            });
            CommandList.Add(new ContextMenuAction
            {
                Displayname = "Remove trend", 
                ContextMenuCommand = TrendRemoveCommand
            });
    
     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 { };
    }