简体   繁体   中英

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?

You could define a Style with a DataTrigger that binds to the IsSelected property of the parent ListBoxItem container:

<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 .

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

 <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

 <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. But your case is much easier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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