【发布时间】:2011-09-13 00:26:00
【问题描述】:
我创建了一个 WPF 应用程序并遵循 MVVM 模式。我的 xaml 中有一个上下文菜单,我需要绑定命令和标题文本。使用下面的代码,我可以将上下文菜单的标题与“MenuItemName”绑定,它是 BOList 中的一个属性,它是一个可观察的集合。我的问题是该命令没有被解雇?我将上下文菜单的项目源更改为 datacontext
(DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}")
命令工作正常,但我的标题变得空白。有没有办法绑定我的标题和菜单项的命令?这里的命令 MenuClick 是 VM 中的 Icommand 属性,而 MenuItemName 是 BOList 中的属性,它是绑定到我的 ListBox 的可观察集合。
<Grid>
<StackPanel Orientation="Vertical">
<Button x:Name="btnClickMe" Command="{Binding ButtonCommand}" Content="Click Me" />
<ListBox ItemsSource="{Binding BOList}" x:Name="lstDemo" SelectedItem="{Binding BussinessObj,Mode=OneWayToSource}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="stkStyleRender" Orientation="Horizontal" Background="Cyan" Width="525" Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" >
<TextBlock x:Name="txtId" Text="{Binding FirstName}"></TextBlock>
<TextBlock x:Name="txtName" Text="{Binding LastName}"></TextBlock>
<StackPanel.ContextMenu>
<ContextMenu x:Name="cntMnuTest" ItemsSource ="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}" >
<MenuItem Header="{Binding MenuItemName}" Command="{Binding MenuClick}" CommandParameter="Icon"></MenuItem>
</ContextMenu>
</StackPanel.ContextMenu>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
【问题讨论】:
-
ViewModel 中的 MenuItemName 是什么样的?在控制台输出中查看是否存在 Beinding Expression 错误,如果看到它,请发布它。
标签: wpf mvvm binding header command