發生在ListView
與自定義ItemContainerStyle
,ItemClick
未被激發,即使IsItemClickEnabled
設置爲True
。我的XAML代碼是一個ResoureDictionary
裏面自定義控件模板,是ListView.ItemClick未在ListView中與自定義ItemContainerStyle
<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被刪除,每件事情都可以正常工作。
您是否找到解決方案?我也有同樣的問題!! – yalematta