Можно обесцветить стандартную функцию выделения ListBox? Может как-то через шаблоны?
Здравствуйте, Аноним, Вы писали:
А>Можно обесцветить стандартную функцию выделения ListBox? Может как-то через шаблоны?
Легко. Без Expression Blend'а алгоритм такой. Скачиваем
ShowMeTheTemplate. Смотрим ControlTemplate для ListBoxItem'а, обнаруживаем вот что:
| Скрытый текст |
| <ControlTemplate TargetType="ListBoxItem" ... >
<Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}"
BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}"
Name="Bd" SnapsToDevicePixels="True">
<ContentPresenter Content="{TemplateBinding ContentControl.Content}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="Selector.IsSelected">
<Setter TargetName="Bd" Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter Property="TextElement.Foreground">
<Setter.Value>
<DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" />
</Setter.Value>
</Setter>
<Trigger.Value>
<s:Boolean>True</s:Boolean>
</Trigger.Value>
</Trigger>
...
</ControlTemplate.Triggers>
</ControlTemplate>
|
| |
А значит переопределив ресурсы кистей с ключами SystemColors.HighlightBrushKey и SystemColors.HighlightTextBrushKey, можем переопределить цвет,
которым рисуется фон и текст выделенного ListBoxItem'а. Например так мы делаем этот фон прозрачным, а цвет текста таким же как и у не выделенного ListBoxItem'а —
в результате, как-будто ничего не выделяется:
<ListBox>
<ListBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{x:Static SystemColors.WindowTextColor}"/>
</ListBox.Resources>
<ListBoxItem>Item1</ListBoxItem>
<ListBoxItem>Item2</ListBoxItem>
<ListBoxItem>Item3</ListBoxItem>
<ListBoxItem>Item4</ListBoxItem>
</ListBox>