2011-07-11 57 views
6

任何人都可以建議我如何實現WPF列表框(有效)有一個透明/命中測試不可見的背景,但其項目仍然命中測試可見?透明WPF列表框與可選項目

換句話說,我希望能夠點擊 - 通過列表框的背景 - 來控制它的下面,但仍然能夠選擇列表框的項目。我有列表框使用自定義佈局面板(這是一個列表框,因爲項目需要可選)。但是,我需要將此面板重疊在其他控件上,以便它們仍能正常使用。

我試過的Background="Transparent"IsHitTestVisible="False"各種組合,但我懷疑我可能是在錯誤的行...

希望這是有道理的 - 我是新來WPF所以任何指導很感激!謝謝。

回答

7

嘗試設置背景,以「{x:空}。在ListeItemContainer和列表框本身

透明仍是打了檢驗的,這就是爲什麼你仍然在它擊中測試

編輯

我跑了WPF督察對樣品,發現ScrollViewer中默認列表框模板是造成鼠標點擊留在列表框。

這裏不包含ScrollViewer的ListBox控件模板。它確實允許鼠標傳遞到列表框後面的TextBox,但仍允許用戶選擇列表框中的項目。

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    > 
    <Window.Resources> 
     <SolidColorBrush x:Key="ListBorder" Color="#828790"/> 
     <Style x:Key="ListBoxStyle1" TargetType="{x:Type ListBox}"> 
      <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
      <Setter Property="BorderBrush" Value="{StaticResource ListBorder}"/> 
      <Setter Property="BorderThickness" Value="1"/> 
      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
      <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/> 
      <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 
      <Setter Property="ScrollViewer.CanContentScroll" Value="true"/> 
      <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
      <Setter Property="VerticalContentAlignment" Value="Center"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ListBox}"> 
         <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true"> 
          <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
         </Border> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsEnabled" Value="false"> 
           <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
          </Trigger> 
          <Trigger Property="IsGrouping" Value="true"> 
           <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Window.Resources> 
    <Grid Width="100"> 
    <TextBox TextWrapping="Wrap">I'm in the background. I'm in the background. I'm in the backgroun.</TextBox> 
    <ListBox Background="{x:Null}" Style="{StaticResource ListBoxStyle1}"> 
     <ListBoxItem Margin="10" Background="#999">Hello</ListBoxItem> 
     <ListBoxItem Margin="10" Background="#999">Hello</ListBoxItem> 
     <ListBoxItem Margin="10" Background="#999">Hello</ListBoxItem> 
     <ListBoxItem Margin="10" Background="#999">Hello</ListBoxItem> 
    </ListBox> 
    </Grid> 
</Window> 
+0

謝謝@Nathan - 我試過了,但那似乎也不奏效。除非我設置了IsHitTestVisible =「False」',但是當然這意味着這些項目是不可選擇的......下面沒有任何東西是可點擊的...... – FuzzyLogic

+0

嘗試使用像Christian Moser的WPF Inspector這樣的工具(http://www.wpftutorial.net/inspector .html)來查看ListBox呈現的圖層/控件。這應該可以幫助您找到需要空背景的圖層。 – NathanAW

+0

我用WPF Inspector研究了這一點,發現ListBox中的ScrollViewer成爲問題。上面是一個從ListBox模板中刪除ScrollViewer的示例。它似乎按照你的要求工作。 – NathanAW