[英]ListBox MenuItem Data template binding
I have he following code: 我有他下面的代码:
<ListBox ItemsSource="{Binding ItemsList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ContextMenu >
<ContextMenu ItemsSource="{Binding MenuItemDataList}">
<DataTemplate DataType="{x:Type model:MenuItemData}" >
<MenuItem Header="{Binding Name}" />
</DataTemplate>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
</Grid>
It throws this exception message when i right click in order to show the pop-up menu: 当我右键单击以显示弹出菜单时,它将引发此异常消息:
Items collection must be empty before using ItemsSource.
ItemsList
and MenuItemDataList
are diferent collections. ItemsList
和MenuItemDataList
是不同的集合。
I think you need to put the datatemplate inside the ContextMenu.ItemTemplate 我认为您需要将datatemplate放在ContextMenu.ItemTemplate中
<ListBox.ContextMenu >
<ContextMenu ItemsSource="{Binding MenuItemDataList}">
<ContextMenu.ItemTemplate>
<DataTemplate DataType="{x:Type model:MenuItemData}">
<MenuItem Header="{Binding Name}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</ListBox.ContextMenu>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.