ListView.ItemClick未在ListView中使用自定义ItemContainerStyle触发

时间:2014-12-21 17:49:26

标签: c# windows-runtime winrt-xaml

在使用自定义ListView的{​​{1}}中,即使ItemContainerStyle设置为ItemClick,也不会触发IsItemClickEnabled。我的XAML代码位于True内部自定义控件模板中,并且是

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

和C#代码是

<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; } 模板ListViewItem属性中的按钮设置为IsHitTestVisible。这不起作用。如果删除了自定义ItemContainersStyle,那么每件事都可以正常工作。

1 个答案:

答案 0 :(得分:1)

这是因为模板中的Button元素阻止ListViewItem处理指针事件。您需要将Button替换为其他元素,例如Border