简体   繁体   English

如何自定义列表框当前选定项的颜色?

[英]How to customize the color of the currently selected item of a listbox?

This question results out of my last question here in stackoverflow.这个问题源于我在 stackoverflow 中的最后一个问题。 The problem to scroll automatically to a specific item is solved, but now i want to set the selected item background to transparent or white.自动滚动到特定项目的问题已解决,但现在我想将所选项目背景设置为透明或白色。 How can i do this??我怎样才能做到这一点??

Update 1更新 1

<ListBox ItemsSource="{Binding SomeData}"
         SelectedIndex="{Binding SomeIndex}">
   <ListBox.ItemsPanel>
      <ItemsPanelTemplate>
         <WrapPanel Orientation="Vertical" />
      </ItemsPanelTemplate>
   </ListBox.ItemsPanel>
   <ListBox.ItemTemplate>
      <DataTemplate>
         <SomeChart DataContext="{Binding }" />
      </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

And for this example i need a way to customize the selecteditem color.对于这个例子,我需要一种方法来自定义 selecteditem 颜色。

Starting with a simple listbox in a window:从窗口中的简单列表框开始:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow"
    Width="525"
    Height="350">
<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>
<Grid Name="LayoutRoot">
    <ListBox ItemsSource="{Binding Elements}"/>
</Grid>

Select the listbox in ExpressionBlend:在 ExpressionBlend 中选择列表框:

在此处输入图片说明

Then select "Object" in the superior menu and choose:然后在上级菜单中选择“对象”并选择:

在此处输入图片说明

This will create a new ListBoxItemStyle and, for the new style, a new ListBoxItem Template, based on the original.这将创建一个新的 ListBoxItemStyle,对于新样式,将创建一个基于原始样式的新 ListBoxItem 模板。 In the default template there is already some logic for changing the visual of the element based in triggers and visualstates.在默认模板中,已经有一些逻辑可以根据触发器和视觉状态更改元素的视觉效果。 The visual of the element when it is selected is driven by a trigger, which you can modify to acomplish your needs.选择元素时的视觉效果由触发器驱动,您可以对其进行修改以满足您的需要。

Select the view triggers.选择视图触发器。 There will some triggers driven by the "IsSelected" property.将有一些由“IsSelected”属性驱动的触发器。 There you can change the appearence of the item when it is selected, selected and disabled, etc.在那里您可以更改项目被选中、选中和禁用等时的外观。

在此处输入图片说明

Just select the trigger you want from the existing ones (for example, the "IsSelected = True") and modify the properties of elements of the template with Blend recording.只需从现有触发器中选择您想要的触发器(例如,“IsSelected = True”)并使用 Blend 记录修改模板元素的属性。 By default, there is some properties changes targeting a "target-element", meaning the template itself, like "Foreground".默认情况下,有一些属性更改针对“目标元素”,即模板本身,例如“前景”。 Others target elements of the template, like the Border "Bd".其他目标是模板的元素,如边框“Bd”。

Of course you can create your ListBoxItemStyle from scratch, but this way is faster if you just want to make simple changes.当然,您可以从头开始创建 ListBoxItemStyle,但如果您只想进行简单的更改,这种方式会更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM