2016-12-29 3 views
0

Я использую System.Windows.Interactivity, и мне нужно отобразить сообщение на вводе мыши. Проблема заключается в том, что одно и то же событие вызывается, когда выполняется вне 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> 

DataContext из элемента в ListView является объектом в вашем «Ur1r2_Obj» ItemsSource тогда DataContext Rectangle - ваша модель взгляда. Вот почему привязка не работает в вашем CellTemplate.

+0

Thankyou так много. –

+0

Thankyou так много. Но как я получу данные, соответствующие этой массивной строке? –

+0

Вы можете передать объект в ItemsSource как CommandArgument для команды: mm8