【问题标题】:ListViewItem not receiving OnMouseDownEvent and ListView not calling selection changed callbackListViewItem 未接收 OnMouseDownEvent 和 ListView 未调用选择更改回调
【发布时间】:2017-05-01 07:38:01
【问题描述】:

我有以下几点:

 <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>

每个单独的“项目”xaml 都有未调用但 OnMouseOver 的 mousedown 回调。

ItemsList_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 事件会被调用。

【问题讨论】:

    标签: c# wpf xaml listview


    【解决方案1】:

    我相信这是因为父 ListViewItem 或 ListView 本身处理 PreviewMouseDown 事件并将其标记为 Handled="True"。

    我有 2 条建议给你:

    1. 不要使用 ListViewItem 作为 ItemTemplate。无论如何,正在创建一个来包装您的 ItemTemplate。

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

    【讨论】:

      猜你喜欢
      • 2014-12-05
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多