2015-09-16 64 views
1

我有一個ListView定製ItemContainerItemTemplate。除其他控件外,ItemTemplate包含一個按鈕。每當我點擊或點擊一個按鈕ListView整個項目被選中,而不是預期的按鈕點擊或點擊事件被髮射。我注意到,按鈕可以單擊的唯一時間是當我將鼠標光標放置在按鈕的頂部邊框時(這也是我獲得默認鼠標懸停效果的唯一時間)。無法在ListView ItemTemplate中處理按鈕水龍頭

這裏是XAML:

<Style TargetType="ListView" x:Key="SalesOrdersListViewStyle"> 
    <Setter Property="HorizontalAlignment" Value="Stretch" /> 
    <Setter Property="VerticalAlignment" Value="Top" /> 
    <Setter Property="SelectionMode" Value="Single" /> 
    <Setter Property="ItemContainerStyle"> 
     <Setter.Value> 
      <Style TargetType="ListViewItem"> 
       <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/> 
       <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/> 
       <!--<Setter Property="Background" Value="Transparent"/>--> 
       <Setter Property="TabNavigation" Value="Local"/> 
       <Setter Property="Padding" Value="12,6"/> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
       <Setter Property="VerticalContentAlignment" Value="Center"/> 
       <Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/> 
       <Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}"/> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="ListViewItem"> 
          <ListViewItemPresenter 
           Foreground="{StaticResource HighlightPressedBrush}" 
           CheckBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}" 
           ContentMargin="{TemplateBinding Padding}" 
           CheckMode="Inline" 
           ContentTransitions="{TemplateBinding ContentTransitions}" 
           CheckBoxBrush="{ThemeResource SystemControlForegroundBaseMediumHighBrush}" 
           DragForeground="{ThemeResource ListViewItemDragForegroundThemeBrush}" 
           DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}" 
           DragBackground="{ThemeResource ListViewItemDragBackgroundThemeBrush}" 
           DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}" 
           FocusBorderBrush="{ThemeResource SystemControlForegroundAltHighBrush}" 
           FocusSecondaryBorderBrush="{ThemeResource SystemControlForegroundBaseHighBrush}" 
           HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
           PointerOverForeground="{StaticResource HighlightPressedBrush}" 
           PressedBackground="{ThemeResource HighlightAlternativePressedBrush}" 
           PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" 
           PointerOverBackground="{ThemeResource HighlightPointerOverBrush}" 
           ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}" 
           SelectedPressedBackground="{ThemeResource ControlBackgroundDarkBrush}" 
           SelectionCheckMarkVisualEnabled="True" 
           SelectedForeground="#FFFFFFFF" 
           SelectedPointerOverBackground="{ThemeResource HighlightAlternativePointerOverBrush}" 
           SelectedBackground="{ThemeResource HighlightPressedBrush}" 
           VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" /> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="ItemTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="Auto" /> 
         <ColumnDefinition Width="Auto" /> 
         <ColumnDefinition Width="100" /> 
         <ColumnDefinition Width="*" /> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition /> 
         <RowDefinition /> 
         <RowDefinition /> 
         <RowDefinition /> 
        </Grid.RowDefinitions> 
        <TextBlock 
         x:Uid="SalesOrderNumber" 
         Grid.Row="0" Grid.Column="1" /> 
        <TextBlock 
         Grid.Row="0" Grid.Column="2" 
         Text="{Binding Number}" /> 

        <!-- TEST BUTTON THAT CANNOT BE TAPPED --> 
        <Button 
         Grid.Row="0" Grid.RowSpan="4" Grid.Column="3" 
         HorizontalAlignment="Left" VerticalAlignment="Center" 
         Background="Red" 
         IsHitTestVisible="True" 
         Content="TEST" /> 

        <TextBlock 
         x:Uid="DeliveryMode" 
         Grid.Row="1" Grid.Column="1" /> 
        <TextBlock 
         Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" 
         Text="{Binding DeliveryMode}"/
        <TextBlock 
         x:Uid="ShippingDate" 
         Grid.Row="2" Grid.Column="1" /> 
        <TextBlock 
         Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2" 
         Text="{Binding ShippingDate}" /> 
        <TextBlock 
         x:Uid="ProjectNumber" 
         Grid.Row="3" Grid.Column="1" /> 
        <TextBlock 
         Grid.Row="3" Grid.Column="2" Grid.ColumnSpan="2" 
         Text="{Binding ProjectNumber}" /> 
       </Grid> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

如何使按鈕點擊或點擊觸發事件,而不是選擇一個項目?

注:我剛剛註釋掉了整個<Setter Property="ItemContainerStyle">,問題仍然存在。

回答

0

問題出在你TextBlocks的Grid.Column="2" Grid.ColumnSpan="2"。將ColumSpan設置爲1,它將起作用。

+0

親愛的上帝之母,爲什麼我沒有看到這個?爲什麼首先有'Grid.ColumnSpan =「2」'?謝謝RavingDev! – Boris

0

我是一個總新手,但難道你只是添加一個按鈕點擊事件?

<Button 
    Grid.Row="0" Grid.RowSpan="4" Grid.Column="3" 
    HorizontalAlignment="Left" VerticalAlignment="Center" 
    Background="Red" 
    IsHitTestVisible="True" 
    Click"btn_Click" 
    Content="TEST" /> 

並在代碼:

private void btn_Click(object sender, RoutedEventArgs e) 
{ 
    // Do stuff 
} 

我希望我不說話瞎扯淡這裏,但我沒有看到任何地方點擊事件。

+0

賈斯珀,謝謝你的回覆。不,問題在於,即使添加了「Click」事件,它也不會被觸發,因爲點擊處理爲「ListView」項目選擇。我之所以沒有'Click'事件是因爲我使用的是MVVM,因此我正在通過指定Command來處理點擊或點擊操作。但問題出現在UI樹視圖和「IsHitTestVisible」屬性中,而不是在實際的事件處理程序中。 – Boris