在具有自定义 ItemContainerStyle
的 ListView
中,即使 IsItemClickEnabled
设置为 True,也不会触发
。我的 XAML 代码位于自定义控件模板内的 ItemClick
ResoureDictionary
中,并且是
<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>
HistoryItemTemplate
是
<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>
C#代码是
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
var historyListView = GetTemplateChild("HistoryListView") as ListView;
if (historyListView != null)
historyListView.ItemClick += OnHistoryItemClicked;
}
我还尝试将 ListViewItem
模板的 IsHitTestVisible
属性中的按钮设置为 False
。这是行不通的。如果删除自定义 ItemContainersStyle,一切都会正常工作。
最佳答案
发生这种情况是因为模板中的 Button
元素阻止 ListViewItem
处理指针事件。您需要将 Button
替换为另一个元素,例如 Border
。
关于c# - ListView.ItemClick 未在具有自定义 ItemContainerStyle 的 ListView 中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27592065/