为什么在listview中不会触发鼠标事件?

时间:2016-12-29 10:23:53

标签: wpf listview events mvvm mouseevent

我正在使用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内部调用它。如何打电话?

1 个答案:

答案 0 :(得分:0)

如果您尝试调用相同的命令(SomeCommand),则应指定Binding的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中的对象,而Rectangle的DataContext是您的视图模型。这就是你的CellTemplate中绑定失败的原因。