c# - 具有可选项目的透明 WPF 列表框

标签 c# wpf listbox transparent

谁能建议我如何实现一个(有效地)具有透明/ HitTest 不可见背景,但其项目仍然 HitTest 可见的 WPF 列表框?

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

我有一个使用自定义布局面板的 ListBox(它是一个 ListBox,因为项目需要可选择)。但是,我需要将此面板覆盖在其他控件之上,以便它们仍能正常使用。

我已经尝试了 Background="Transparent"IsHitTestVisible="False" 的各种组合,但我怀疑我可能走错了路...

希望这是有道理的 - 我是 WPF 的新手,因此非常感谢任何指导!谢谢。

最佳答案

尝试将 ListeItemContainer 和 ListBox 本身的背景设置为“{x:Null}”。

Transparent 仍然是可 HitTest 的,这就是为什么您仍在对其进行 HitTest 的原因。

编辑

我在示例上运行 WPF Inspector,发现默认 ListBox 模板中的 ScrollViewer 导致鼠标单击停留在 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>

关于c# - 具有可选项目的透明 WPF 列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6652745/

相关文章:

c# - WCF 和 Silverlight - 二进制编码

c# - 从 DLL 更新 WPF GUI

c# - 绑定(bind)到静态类属性和 StringFormat

c# - Razor动态表创建c#

c# - 在 C# 中从列表 <> 中删除对象

c# - 亚马逊 S3 : How to get a list of folders in the bucket?

wpf - 如何将 ViewModel 的 ObservableCollection 绑定(bind)到 MenuItem?

c# - 使用 Silverlight 获取列表框中多个选定项的索引

WPF ListBox 失去焦点后将焦点设置为第一项

c# - 在焦点范围内使用列表框