WPF ListBox:垂直滚动条和焦点
WPF ListBox: Vertical scrollbar and focus
我有两个关于 ListBox/WrapPanel 的问题。
首先,我的设置:
在 XAML 我有一个列表框。 ListBox 内部是一个 WrapPanel。我以编程方式将 ListBoxItems 添加到 WrapPanel。
问题一
ListBoxItems [StackPanel with Image and TextBlock] 从左到右、从上到下填充。但是,如果 ListBoxItems 的数量多于 ListBox 中的 space,则不会出现垂直滚动条。以下 XAML 代码使垂直滚动条可见。但是,它仍处于禁用状态:
<ListBox Name="li1StandortLinks" Background="Transparent" MaxHeight="300" ScrollViewer.VerticalScrollBarVisibility="Visible">
<WrapPanel Name="wp1StandortLinks" ItemHeight="80" ItemWidth="150" Width="755" />
</ListBox>
如何在需要时获得功能性垂直滚动条?
问题二
当单击 ListBox 而不是 ListBoxItem 时 [例如,在它们之间],整个 ListBox 的背景以蓝色突出显示。最初背景是透明的,即使单击 ListBox 本身,它也应该保持这种状态。捕捉点击事件并将背景设置回透明似乎无济于事。蓝色高亮颜色确实消失了,但 ListBox 背景并没有变回透明,而是保持灰色。
我可以更改样式,以便在单击列表框时颜色完全不变吗?不过,我从未真正更改过 WPF 中的样式,因此我需要一个准确的解释。
这可能适用于问题一:
<ListBox Name="li1StandortLinks" Background="Transparent" MaxHeight="300" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
对于问题 2:
这也可能有效(更改列表框的样式)
<ListBox Name="li1StandortLinks" Background="Transparent" MaxHeight="300" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.Style>
<Style TargetType="ListBox">
<Style.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
</Style.Resources>
</Style>
</ListBox.Style>
</ListBox>
我有两个关于 ListBox/WrapPanel 的问题。
首先,我的设置: 在 XAML 我有一个列表框。 ListBox 内部是一个 WrapPanel。我以编程方式将 ListBoxItems 添加到 WrapPanel。
问题一
ListBoxItems [StackPanel with Image and TextBlock] 从左到右、从上到下填充。但是,如果 ListBoxItems 的数量多于 ListBox 中的 space,则不会出现垂直滚动条。以下 XAML 代码使垂直滚动条可见。但是,它仍处于禁用状态:
<ListBox Name="li1StandortLinks" Background="Transparent" MaxHeight="300" ScrollViewer.VerticalScrollBarVisibility="Visible">
<WrapPanel Name="wp1StandortLinks" ItemHeight="80" ItemWidth="150" Width="755" />
</ListBox>
如何在需要时获得功能性垂直滚动条?
问题二
当单击 ListBox 而不是 ListBoxItem 时 [例如,在它们之间],整个 ListBox 的背景以蓝色突出显示。最初背景是透明的,即使单击 ListBox 本身,它也应该保持这种状态。捕捉点击事件并将背景设置回透明似乎无济于事。蓝色高亮颜色确实消失了,但 ListBox 背景并没有变回透明,而是保持灰色。 我可以更改样式,以便在单击列表框时颜色完全不变吗?不过,我从未真正更改过 WPF 中的样式,因此我需要一个准确的解释。
这可能适用于问题一:
<ListBox Name="li1StandortLinks" Background="Transparent" MaxHeight="300" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
对于问题 2: 这也可能有效(更改列表框的样式)
<ListBox Name="li1StandortLinks" Background="Transparent" MaxHeight="300" ScrollViewer.VerticalScrollBarVisibility="Visible">
<ListBox.Style>
<Style TargetType="ListBox">
<Style.Resources>
<!-- Background of selected item when focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
<!-- Background of selected item when not focussed -->
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
</Style.Resources>
</Style>
</ListBox.Style>
</ListBox>