ListView.ItemClick not launched on ListView with custom ItemContainerStyle

As ListView

a user ItemContainerStyle

, ItemClick

not started, even IsItemClickEnabled

set in True

. My XAML is in ResoureDictionary

inside a custom control template and

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

      

and HistoryItemTemplate

there is

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

      

and c # code

protected override void OnApplyTemplate()
{
    base.OnApplyTemplate();

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

      

I am also trying to set a button in ListViewItem

Template IsHitTestVisible

to False

. This does not work. Everything works fine if the custom ItemContainersStyle is removed.

+3


source to share


1 answer


This is because the element Button

in your template is blocking ListViewItem

from handling pointer events. You need to replace with Button

another element eg Border

.



+1


source







All Articles