C# 子菜单项上的WPF上下文菜单绑定项源
我的C# 子菜单项上的WPF上下文菜单绑定项源,c#,wpf,xaml,C#,Wpf,Xaml,我的XAML <ContextMenu x:Key="ExportContext"> <MenuItem Header="Export Journey" Command="{Binding ExportJourney}"/> <MenuItem Header="Export..." ItemsSource="{Binding SelectedTrip.Cl
XAML
<ContextMenu x:Key="ExportContext">
<MenuItem Header="Export Journey"
Command="{Binding ExportJourney}"/>
<MenuItem Header="Export..."
ItemsSource="{Binding SelectedTrip.Clips}">
<MenuItem.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding Name}"/>
</DataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
</ContextMenu>
旅程
类包含一个属性剪辑
,它是一个列表
。然后,Clip
类包含要显示的Name
的字符串属性
JourneyList
显示在DataGrid
中,当选择一行时,Journey
被设置为SelectedTrip
我不明白为什么绑定到ExportJourney
ICommand
可以完美地工作,但导出下没有显示任何内容。。。标题。
我唯一能想到的是,SelectedJourney
在加载时为空
如果出于测试目的,我绑定到JourneyList
而不是SelectedTrip.Clips
绑定工作正常,我会得到一个子菜单(虽然没有名字,但位置在那里!)
不确定如何调试这个?非常感谢关于调试绑定的任何建议,或者关于我做错了什么的建议 在不到10分钟的时间里回答了我自己的问题,但我花了一周的时间在谷歌上把它发布到这里。我讨厌这个世界有时的运作方式 回答:
ContextMenu
上的ItemsSource
不能是列表
类型
我调整了我的旅程
类,使publicClip
属性成为IEnumerable
(privateClip
属性成为可观察集合
),而不是列表
namespace ViewModel
{
class HomeViewModel: ObservableObject, IPageViewModel
{
private ICommand _exportJourney;
private ObservableCollection<Journey> _journeyList = new ObservableCollection<Journey>();
private Journey _selectedTrip;
public IEnumerable<Journey> JourneyList
{
get { return _journeyList; }
}
public Journey SelectedTrip
{
get { return _selectedTrip; }
set
{
if (_selectedTrip != value)
{
_selectedTrip = value;
_journeyList.Where(c => c.Selected == true).ToList().ForEach(a => a.Selected = false);
_journeyList.Where(c => c.StartTime == value.StartTime).ToList().ForEach(a => a.Selected = true);
OnPropertyChanged("SelectedTrip");
}
}
}
public ICommand ExportJourney
{
get
{
if (_exportJourney == null)
{
_exportJourney = new RelayCommand(
p => ExtractJourney(),
p => SelectedTrip != null);
}
return _exportJourney;
}
}
}
}