Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListViewItem未接收OnMouseDownEvent,ListView未调用选择更改回调_C#_Wpf_Xaml_Listview - Fatal编程技术网

C# ListViewItem未接收OnMouseDownEvent,ListView未调用选择更改回调

C# ListViewItem未接收OnMouseDownEvent,ListView未调用选择更改回调,c#,wpf,xaml,listview,C#,Wpf,Xaml,Listview,我有以下资料: <ListView SelectionMode="Multiple" SelectedIndex="{Binding SelectedIdx}" SelectionChanged="ItemsList_SelectionChanged" MinHeight="200" x:Name="ItemsList" ItemsSource="{Binding Items}" Background="Yellow" Grid.Row="1">

我有以下资料:

 <ListView SelectionMode="Multiple" SelectedIndex="{Binding SelectedIdx}" SelectionChanged="ItemsList_SelectionChanged"  MinHeight="200" x:Name="ItemsList" ItemsSource="{Binding Items}" Background="Yellow" Grid.Row="1">
                    <ListView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <!--  note that the list mode is wide enough to force a wrap to each new line  -->
                            <WrapPanel Width="{Binding ActualWidth, ElementName=ItemsList}" />
                        </ItemsPanelTemplate>
                    </ListView.ItemsPanel>

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <local:Item/>
                        </DataTemplate>
                    </ListView.ItemTemplate>

                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="Padding" Value="0"/>
                            <Setter Property="Margin" Value="0"/>
                        </Style>
                    </ListView.ItemContainerStyle>
                </ListView>

每个单独的“Item”xaml都有mousedown回调,这些回调不会被调用,但OnMouseOver会被调用

ItemsList\u SelectionChanged也不会被调用

xaml项非常基本:

<ListViewItem x:Class="Controls.Item"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Controls"
             mc:Ignorable="d" 
             xmlns:ViewModel="clr-namespace:Controls.Controls.Item.ViewModel"
             d:DesignHeight="50" d:DesignWidth="50"
             Width="50"
             Height="50"
             Background="Blue"
             Padding="0,0,0,0"
         MouseLeftButtonDown="ListViewItem_MouseLeftButtonDown">
   <!--  <Grid>
        < ! - - <Label IsHitTestVisible="False"  Width="50" Height="50" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" FontSize="10" Background="Pink"  Content="{Binding Label}"/> - - >
    </Grid> -->
</ListViewItem>


正如我之前所说,“ListViewItem_MouseLeftButtonDown”也不会被调用,但如果有MouseOver事件被调用

我认为这是因为父ListViewItem或ListView本身处理PreviewMouseDown事件,并将其标记为Handled=“True”

我有两个建议:

  • 不要将ListViewItem用作ItemTemplate。正在创建一个来包装ItemTemplate

  • 编写用于ItemTemplate内容的行为。该行为应注册到PreviewMouseDown事件并引发一个命令(从ListView的DataContext绑定到该命令)