WPF - FocusVisualStyle我应该在哪里应用它?

3

我有一个 UserControl,基本上像这样包装了一个 ListBox -

        <ListBox x:Name="lb" ItemsSource="{Binding ElementName=UC,Path=Pages}"
             Background="{Binding ElementName=UC,Path=Background}"
             BorderBrush="Transparent"
             ScrollViewer.CanContentScroll="False" 
             ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
             ScrollViewer.VerticalScrollBarVisibility="Disabled">

        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" IsItemsHost="True"/>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Width="{Binding ElementName=UC,Path=ActualWidth}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition MinWidth="20"/>
                        <ColumnDefinition/>
                        <ColumnDefinition MinWidth="20"/>
                    </Grid.ColumnDefinitions>
                    <ContentPresenter Grid.Column="1" Content="{Binding}"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

我需要将FocusVisualStyle设置为{x:Null}来隐藏此功能,但无论我在哪里应用它,我仍然会得到默认的蓝色选择颜色。我尝试在ListBox、StackPanel和Grid上设置它,但都没有成功。
希望能得到帮助,谢谢。
2个回答

10

FocusVisualStyle 应用于焦点元素的 "虚线框",而不是背景色。 要更改选定 ListBoxItems 的背景色,请执行以下操作:

<ListBox>
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Value="Red"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Value="Black"/>
    </ListBox.Resources>    
</ListBox>

2
肯特是正确的,FocusVisualStyle 只与使用 Tab 键选择控件时的键盘焦点相关。
如果您只想显示一个列表而没有任何选择功能,您可以将 ListBox 降级为 ItemsControl。
<ItemsControl x:Name="lb" ItemsSource="{Binding ElementName=UC,Path=Pages}" 
  Background="{Binding ElementName=UC,Path=Background}" 
  BorderBrush="Transparent" ScrollViewer.CanContentScroll="False" 
  ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
  ScrollViewer.VerticalScrollBarVisibility="Disabled">
  <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" IsItemsHost="True"/>
        </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <!-- others -->
</ItemsControl>

是的,我试过了,但我在ListBox中呈现UIElements,它们不遵守ItemsControl中的ItemTemplate。 不过还是谢谢你的帮助。 - EightyOne Unite

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