2016-12-29 79 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

相關問題