【发布时间】:2015-04-17 10:07:27
【问题描述】:
我正在与 WPF 中的 ListBox 控件样式作斗争。
如果我的鼠标悬停在 ListBoxItem 上,我想更改项目的 BorderBrush 属性。
我的 ListBox 是自定义控件的一部分,但这里有一些代码:
<ListBox x:Name="suggestionListBox"
SelectionChanged="suggestionListBox_SelectionChanged"
MouseUp="SuggestionListBox_OnMouseDown"
Background="{Binding ElementName=Control, Path=Background}"
ItemTemplate="{Binding ItemTemplate, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type controls:AutoCompleteComboBox}}}"
Width="{Binding ElementName=Control, Path=ActualWidth}"
HorizontalContentAlignment="Stretch">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="BorderThickness" Value="1"></Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderBrush" Value="Yellow" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
我正在像这样添加 ItemTemplate:
<DataTemplate>
<StackPanel>
<Label Content="{Binding FullName}" />
</StackPanel>
</DataTemplate>
基本上,ListBoxItem 里面有边框,我无法访问,当 IsMouseOver 设置为 true 时,边框会发生变化。
鼠标悬停时如何改变边框的颜色?
【问题讨论】:
-
默认 ListBoxItem 样式中的 ControlTemplate 可能只是不使用 BorderBrush 属性。您应该创建自己的 ControlTemplate。看看 MSDN 上的ListBox Styles and Templates 页面。
-
-
@Jayasri 我应该把这个放在哪里?
-
xaml 页面内的样式部分
-
好的,非常感谢