带有可选项的透明WPF ListBox

Fuz*_*gic 6 c# wpf listbox transparent

任何人都可以建议我如何实现一个WPF ListBox(有效地)具有透明/命中测试不可见的背景,但其项目仍然是命中测试可见的?

换句话说,我希望能够通过ListBox的背景点击它下面的控件,但仍然可以选择ListBox的项目.

我有ListBox使用自定义布局面板(它是一个ListBox,因为项目需要是可选择的).但是,我需要将此面板叠加在其他控件的顶部,以允许它们仍然正常使用.

我已经试过的各种组合Background="Transparent"IsHitTestVisible="False",但我怀疑我可能是在错误的行...

希望这是有道理的 - 我是WPF的新手,所以任何指导都将非常受欢迎!谢谢.

Nat*_*nAW 7

尝试在ListeItemContainer和ListBox本身上将背景设置为"{x:Null}".

透明仍然是可测试的,这就是为什么你还在接受命中测试的原因.

编辑

我在一个示例上运行了WPF Inspector,发现默认ListBox模板中的ScrollViewer导致鼠标单击停留在ListBox中.

这是一个不包含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>
Run Code Online (Sandbox Code Playgroud)