Wpf lisbox中所选项目的样式

Wpf lisbox中所选项目的样式,wpf,xaml,listbox,Wpf,Xaml,Listbox,我有一个绑定到字符串列表的列表框 <ListBox Grid.Row="1" Height="130" Background="Black" BorderThickness="0" ItemsSource="{Binding Images}" ItemTemplate="{StaticResource PanoItemTemplate}"

我有一个绑定到字符串列表的列表框

 <ListBox Grid.Row="1"
                 Height="130"
                 Background="Black" BorderThickness="0"
                 ItemsSource="{Binding Images}"
                 ItemTemplate="{StaticResource PanoItemTemplate}"
                 SelectedItem="{Binding SelectedImage}">

            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"
                               Height="110"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
</ListBox>

在VM中,我有:

public ObservableCollection<string> Images
{
  get { return _images; }
}

public string SelectedImage
{
  get { return _selectedImage; }
  set
  {
    _selectedImage = value;
    OnPropertyChanged("SelectedImage");
  }
}
公共可观测采集图像
{
获取{return\u images;}
}
公共字符串SelectedImage
{
获取{return}
设置
{
_选择图像=值;
OnPropertyChanged(“SelectedImage”);
}
}
当我填充图像列表并通过单击列表框在列表框中选择其中一个时,我可以在SelectedImage中获得它,系统运行良好

当我点击列表框中的一个项目时,它显示为selected I ListBox(白色背景上的蓝色)

如果我将SelectedImage in code设置为图像列表中的项目,则该项目在列表中处于选中状态,但颜色不同(白色背景上为白色)


当我通过代码选择selectedImage时,如何将其样式更改为与用户选择时相同?

当列表框具有用户焦点时,
将仅高亮显示蓝色,否则它将使用不同的画笔

当列表框被聚焦时,它使用
SystemColors.HighlightTextBrushKey
,而未聚焦时,它使用
SystemColors.inactiveselection highlightbrushkey

<ListBox >
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>
    </ListBox.Resources>
</ListBox>
因此,您可以将
SystemColors.inactiveselection highlightbrushkey
设置为
SystemColors.HighlightColor
,这将使其在焦点处保持蓝色

例如:

<ListBox >
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="{x:Static SystemColors.HighlightColor}"/>
    </ListBox.Resources>
</ListBox>