C# ListView.ItemClick未在具有自定义ItemContainerStyle的ListView中激发

C# ListView.ItemClick未在具有自定义ItemContainerStyle的ListView中激发,c#,windows-runtime,winrt-xaml,C#,Windows Runtime,Winrt Xaml,在带有自定义ItemContainerStyle的ListView中,即使IsItemClickEnabled设置为True,也不会激发ItemClick。我的XAML代码位于自定义控件模板内的resourceDictionary中,并且 <ListView ItemsSource="{TemplateBinding History}" ItemTemplate="{StaticResource HistoryItemTemplate}" ItemC

在带有自定义
ItemContainerStyle
ListView
中,即使
IsItemClickEnabled
设置为
True
,也不会激发
ItemClick
。我的XAML代码位于自定义控件模板内的
resourceDictionary
中,并且

<ListView ItemsSource="{TemplateBinding History}"
          ItemTemplate="{StaticResource HistoryItemTemplate}"
          ItemContainerStyle="{StaticResource HistoryItemContainerStyle}"
          IsHitTestVisible="True" SelectionMode="None" IsSwipeEnabled="False"
          CanDragItems="False" CanReorderItems="False"
          IsItemClickEnabled="True" Width="400" Margin="50,0,0,0" >
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Margin="0" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>
C#代码是


我还尝试将
ListViewItem
模板的
ishitestvisible
属性中的按钮设置为
False
。这行不通。如果删除自定义ItemContainerStyle,一切都会正常工作。

发生这种情况是因为模板中的
按钮
元素阻止
列表视图项处理指针事件。您需要将
按钮
替换为另一个元素,例如
边框

我需要该按钮来响应指针越过和按下的视觉效果States@AdnanUmer不幸的是,并没有办法用按钮来做。因此,
ListViewItem
处于按下状态。不确定是否有
指针,您可以尝试一下,尽管
ListBoxItem
两者都有。您找到解决方案了吗?我也有同样的问题!!
<Style x:Key="HistoryItemContainerStyle" TargetType="ListViewItem">
    <Setter Property="BorderThickness" Value="0,0,0,1" />        
    <Setter Property="Margin" Value="0" />
    <Setter Property="Padding" Value="0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <Button Style="{StaticResource HistoryItemButtonStyle}"
                        Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" />                    
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
protected override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    var historyListView = GetTemplateChild("HistoryListView") as ListView;
    if (historyListView != null)
        historyListView.ItemClick += OnHistoryItemClicked;
}