2016-12-29 95 views
0

我正在使用System.Windows.Interactivity,我必须在鼠标输入事件中显示消息。 问题是在ListView外部完成时调用的相同事件,但在ListView内部时不会调用它。为什么在listview内部没有触发鼠标事件?

我试着这样做使用MVVM方法:

<Grid> 
    <StackPanel Grid.Row="1" Grid.Column="1" Name="events1"> 
     //This Event works outside the ListView but not inside ListView 
     <Rectangle Fill="Yellow" Stroke="Black" Width="100" Height="30"> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="MouseEnter"> 
        <i:InvokeCommandAction Command="{Binding SomeCommand}" /> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
     </Rectangle> 
     <ListView BorderBrush="#FF1D93E4" BorderThickness="4" Behaviour:GridViewColumnResize.Enabled="True" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectedItem="{Binding SelectedItem ,Mode=TwoWay}" ItemsSource="{Binding Ur1r2_Obj}" HorizontalContentAlignment="Stretch"> 
      <ListView.ItemContainerStyle> 
       <Style TargetType="ListViewItem"> 
        <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
       </Style> 
      </ListView.ItemContainerStyle> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Header="State" Behaviour:GridViewColumnResize.Width="*"> 
         <GridViewColumn.CellTemplate> 
          <DataTemplate> 
           <Grid Background="{Binding Converter={StaticResource colorToBrushConverter}}"> 
            //The event do not work here 
            <i:Interaction.Triggers> 
             <i:EventTrigger EventName="MouseEnter"> 
              <i:InvokeCommandAction Command="{Binding SomeCommand}" /> 
             </i:EventTrigger> 
            </i:Interaction.Triggers> 

            <Button Margin="35,0,0,0" HorizontalAlignment="Center" Content="{Binding Path=Etat, Mode=OneWay}" Background="{Binding Converter={StaticResource colorToBrushConverter}}" /> 
           </Grid> 
          </DataTemplate> 
         </GridViewColumn.CellTemplate> 
        </GridViewColumn> 
       </GridView> 
      </ListView.View> 
     </ListView> 
    </StackPanel> 
</Grid> 

鉴于型号

private ICommand someCommand; 
     public ICommand SomeCommand 
     { 
      get 
      { 
       //return plotButton; 
       return someCommand ?? (someCommand = new CommandHandler(() => MyAction2(), _canExecute)); 
      } 
      set 
      { 
       someCommand = value; 
       OnPropertyChanged("SomeCommand"); 

      } 
     } 

     private void MyAction2() //Messsage is popuped on Moseenter event when i try to hover mouse over rectangle outside the listview , But not get called when inside the Listview 
     { 
      MessageBox.Show("Yeah Called"); 
     } 

为什么事件不会从ListView控件中调用。如何打电话?

回答

0

如果你想调用相同的命令(SomeCommand),你应该指定绑定的的RelativeSource:

<Grid Background="{Binding Converter={StaticResource colorToBrushConverter}}"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="MouseEnter"> 
      <i:InvokeCommandAction Command="{Binding DataContext.SomeCommand, 
                RelativeSource={RelativeSource AncestorType=ListView}}" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
    <Button Margin="35,0,0,0" HorizontalAlignment="Center" Content="{Binding Path=Etat, Mode=OneWay}" Background="{Binding Converter={StaticResource colorToBrushConverter}}" /> 
</Grid> 

在ListView项目的DataContext的是你的“Ur1r2_Obj”的ItemsSource,而对象矩形的DataContext是您的视图模型。这就是为什么你的CellTemplate中绑定失败。

+0

谢谢这么多。 –

+0

谢谢你这么多。但是,我将如何获得相应的鼠标中心行数据? –

+0

您可以将ItemsSource中的对象作为CommandArgument传递给该命令: mm8

相关问题