[英]WPF XAML listbox selected item border color
I have implemented some listbox which contains border and a grid in this border. 我实现了一些列表框,其中包含边框和此边框中的网格。
<Style x:Key="SelectedHiglightStyle"
TargetType="{x:Type ListBoxItem}"
BasedOn="{StaticResource MaterialDesignListBoxItem}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
Color="Transparent" />
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="Transparent" />
</Style.Resources>
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="#316308" />
<Setter Property="Opacity"
Value="0.8" />
</Trigger>
</Style.Triggers>
</Style>
<ListBox IsSynchronizedWithCurrentItem="True"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Grid.Row="3"
ScrollViewer.CanContentScroll="False"
Style="{StaticResource MaterialDesignListBox}"
ItemsSource="{Binding Devices}"
SelectedItem="{Binding SelectedDevice, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
ItemContainerStyle="{StaticResource SelectedHiglightStyle}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Rectangle Width="35"
Height="35"
Margin="5"
HorizontalAlignment="Left"
OpacityMask="{DynamicResource DashboardDeviceLogo}">
<Rectangle.Fill>
................
<Grid Grid.Column="1">
<StackPanel>
<TextBlock Text="{lex:Loc DeviceName}"
Margin="0,4,0,2" />
<TextBlock x:Name="tbDeviceName"
Text="{Binding Device.Name}"
FontSize="10" />
................
How I can change color of selected item border? 如何更改所选项目边框的颜色? Each item has his own view-model. 每个项目都有自己的视图模型。 Is there a easier way than broadcast message via Messanger (I'm using MVVM Light) , capture it in all DeviceViewModel's
, compare id of device and then bind the color from view-model? 是否有比通过Messanger广播消息更简单的方法(我正在使用MVVM Light),将其捕获到所有DeviceViewModel's
,比较设备的ID,然后从视图模型中绑定颜色?
You could define a Style
with a DataTrigger
that binds to the IsSelected
property of the parent ListBoxItem
container: 您可以使用绑定到父ListBoxItem
容器的IsSelected
属性的DataTrigger
定义Style
:
<ListBox.ItemTemplate>
<DataTemplate>
<Border>
<Border.Style>
<Style TargetType="Border">
<Setter Property="BorderThickness" Value="2" />
<Setter Property="BorderBrush" Value="Black" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
Value="True">
<Setter Property="BorderBrush" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Grid>
...
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
The Style
is applied to the Border
element in your ItemTemplate
. Style
将应用于ItemTemplate
的Border
元素。
The easiest way to do this is here. 最简单的方法是在这里。
If you need just to change selection border you write this in listboxitem's style triggers section 如果只需要更改选择边框,请在listboxitem的样式触发器部分中编写
<Trigger Property="IsSelected" Value="True">
<!--your code...-->
<Setter Property="BorderBrush"
Value="Red"/>
<Setter Property="BorderThickness" Value="1"/>
</Trigger>
And edit your datatemplate, binding your border to setters of borderbrush and borderthickness in style 并编辑数据模板,将边框绑定到样式的borderbrush和borderthickness的设置器上
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">...
I've edited my answer cause first time I read this question, I thought you need different borderbrush for each type of data. 第一次阅读此问题时,我已经编辑了答案,我认为每种数据类型都需要使用不同的Borderbrush。 But your case is much easier 但是你的案子要容易得多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.