具有可选项的透明WPF ListBox

6
有没有人可以建议我如何实现一个WPF ListBox,它具有透明/ hit-test-invisible 背景,但其项目仍然是 hit-test-visible 的?
换句话说,我想能够通过 ListBox 的背景点击到它下面的控件,但仍然能够选择 ListBox 的项目。
我有一个使用自定义布局面板的 ListBox(因为需要选择项目)。然而,我需要将此面板覆盖在其他控件上方,允许它们正常使用。
我尝试了各种组合的 Background="Transparent" 和 IsHitTestVisible="False",但我怀疑我可能走错了路线...
希望这有意义 - 我是 WPF 新手,所以任何指导都将不胜感激!谢谢。
1个回答

7
尝试将ListeItemContainer和ListBox本身的背景设置为"{x:Null}"。透明仍然可以进行命中测试,这就是为什么您仍然会在其上得到命中测试的原因。
编辑:
我在样本上运行了WPF Inspector,并发现默认ListBox模板中的ScrollViewer导致鼠标点击留在了ListBox中。
以下是一个ListBox控件模板,它不包括ScrollViewer。它允许鼠标穿过位于listbox后面的文本框,并且仍允许用户选择列表框中的项目。
<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>

谢谢@Nathan - 我尝试了,但似乎也不起作用。除非我设置IsHitTestVisible="False",否则下面的内容都无法点击,但这当然意味着这些项目是不可选择的... - FuzzyLogic
尝试使用像Christian Moser的WPF Inspector(http://www.wpftutorial.net/inspector.html)这样的工具,查看ListBox正在呈现哪些层/控件。这应该会帮助您定位需要具有空背景的图层。 - NathanAW
我使用WPF Inspector进行了调查,并发现ListBox中的ScrollViewer是问题所在。上面是一个示例,它从ListBox模板中删除了ScrollViewer。它似乎按照您的要求工作。 - NathanAW
太棒了!那太好了,Nathan,非常感谢。我刚刚看了WPF Inspector和Snoop(我以前不知道),但从未考虑过ScrollViewer。干杯! - FuzzyLogic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接