【发布时间】: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 { };
}
【问题讨论】: