问题描述
C# WPF MVVM ListViewItem的ContextMenu的MenuItem中Command绑定的CommandBase事件无响应。
代码逻辑描述
创建了一个MainWindow,在MainWindow中间划定一块区域用以绑定显示其他的View界面;
我在一个View(MonitorView)中创了一个ListView,每一个Items都有几个操作,我将这几个操作定义在了ContextMenu中,当鼠标右击时就显示ContextMenu,点击MenuItem执行对应的操作;
<!-- ContextMenu -->>
<ContextMenu x:Key="ContextMenu1">
<MenuItem Header="出厂" FontSize="14" >
<MenuItem Header="下行" FontSize="14" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl},Path=DataContext.DownOutTrainCommand}"
CommandParameter="{Binding}"/>
<Separator/>
<MenuItem Header="上行" FontSize="14"/>
</MenuItem>
<Separator/>
<MenuItem Header="去15" FontSize="14" Command="{Binding DataContext.DownOutTrainCommand,RelativeSource={RelativeSource AncestorType={x:Type local:MonitorView}}}"
CommandParameter="{Binding }"/>
<MenuItem Header="去14" FontSize="14" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl},Path=DataContext.DownOutTrainCommand}"
CommandParameter="{Binding}" />
<MenuItem Header="去13" FontSize="14" Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl},Path=DataContext.DownOutTrainCommand}"
CommandParameter="{Binding}" />
</ContextMenu>
ListViewItem 的 ItemContainerStyle的样式
<Style x:Key="ListItemType" TargetType="{x:Type ListViewItem}">
<Setter Property="Margin" Value="2,10,2,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border BorderThickness="1" CornerRadius="5" x:Name="borderground" >
<Border.Background>
<ImageBrush ImageSource="pack://application:,,,/huainan_railway;component/Assets/Image/train.png" Stretch="UniformToFill"/>
</Border.Background>
<Border.Effect>
<DropShadowEffect Color="#FF1F0F0B" BlurRadius="10" Opacity="0.5"></DropShadowEffect>
</Border.Effect>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" Background="#2c6cf3" Width="18" Height="18" CornerRadius="9" Margin="2,0,3,-7" BorderBrush="White" BorderThickness="1" HorizontalAlignment="Left">
<TextBlock Text="{Binding StockIndex}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16" Foreground="White"/>
</Border>
<Border Grid.Row="1" Margin="5,0" Background="Transparent" Name="root" BorderThickness="0,0,0,1" CornerRadius="6">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding MachineType}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16" Foreground="#FF17FF00" FontWeight="Bold"/>
<TextBlock Grid.Column="1" Text="{Binding TrainNumber}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16" Foreground="#FF17FF00" FontWeight="Bold"/>
</Grid>
</Border>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContextMenu" Value="{StaticResource ContextMenu1}"/>
<Setter Property="ToolTip" Value="{Binding Source={StaticResource ItempInfoTip}}"/>
</Style>
ListView 调用
<ListView ItemsSource="{Binding trainsSolid.trains5}" Background="Transparent"
BorderThickness="1,1,1,1" Style="{StaticResource ListBorderBrush}"
ItemContainerStyle="{StaticResource ListItemType}"
Height="55" Width="461" Canvas.Top="610" Canvas.Left="743">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
ViewModel中的事件
//出车 下行
private CommandBase _downOutTrainCommand;
public CommandBase DownOutTrainCommand
{
get
{
if (_downOutTrainCommand == null)
{
_downOutTrainCommand = new CommandBase();
_downOutTrainCommand.DoExecute = new Action<object>(obj =>
{
try
{
TrainModel trainModel = (TrainModel)obj;
//TrainPlanModel trainPlanModel = (TrainPlanModel)obj;
//monitorService.UpdatePlanState(3, trainPlanModel.ID);
//trainPlanModel.Status = 3;
//ObservableCollection<TrainModel> tM = trainsSolid.GetObservable(trainPlanModel.Stock);
//monitorService.insertTrain(trainPlanModel, tM);
Initialize();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
});
}
return _downOutTrainCommand;
}
}
分析
要明确指定数据源 无法通过继承拿到? 但是不知道怎么写;加载ViewModel? 尝试了不行,不知道怎么回事;
<<ResourceDictionary >
<vm:MonitorViewModel x:Key="mvm"/>
</ResourceDictionary>
<UserControl.DataContext>
<Binding Source="{StaticResource mvm}"/>
</UserControl.DataContext>
<UserControl.ContextMenu>
<ContextMenu DataContext="{StaticResource mvm}"/>
</UserControl.ContextMenu>