2016-12-29 10 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のは、あなたの「Ur1r2_Obj」のItemsSourceのに対し内のオブジェクトでありますRectangleのDataContextがビューモデルです。そのため、CellTemplateではバインディングが失敗します。

+0

ありがとうございます。 –

+0

ありがとうございます。しかし、どのように私はそのmousentered行に対応するデータを取得するのですか? –

+0

ItemsSourceのオブジェクトをCommandArgumentとしてコマンドに渡すことができます。 mm8

関連する問題