C# MVVM列表框项目上下文菜单
我在绑定listbox项的contextmenu时遇到问题。生成后,我的上下文菜单将不显示任何项目。我搜索了很多,但没有任何积极的结果。Contextmenu仍为空。你知道我的问题有什么解决办法吗 谢谢你的帮助 列表框: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 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 { };
}
菜单->调试->窗口->输出
)。如果绑定中有错误,应该在这里看到CommandList
属性?它应该出现在你的“趋势”课上,或者你管它叫什么CommandList
属性上有公共setter?
这可能不会引起问题,但通常您要么只是从集合中添加/删除项,要么不修改集合,而是始终设置集合的新实例。你们班两个都允许,这有点异味菜单->调试->窗口->输出
)。如果绑定中有错误,应该在这里看到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 { };
}