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