2014-12-21 69 views
2

發生在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被刪除,每件事情都可以正常工作。

+0

您是否找到解決方案?我也有同樣的問題!! – yalematta

回答

1

發生這種情況是因爲您的模板中的Button元素阻止了ListViewItem處理指針事件。您需要將Button替換爲另一個元素,例如Border

+0

我需要該按鈕來響應PointerOver和按下視覺狀態 –

+0

@AdnanUmer不幸的是,沒有辦法使用按鈕。因此'ListViewItem'具有'按下'狀態。不確定'PointerOver',你可以嘗試一下,儘管'ListBoxItem'具有明確的功能。 –