Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a border on a list box item

Tags:

c#

wpf

xaml

I have a ListBox in which the items are created from an item source, I need every item to have a border but I have no clue where I even set a style for ListBox items.

<ListBox x:Name="m_list">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Expander IsExpanded="True">
                <CustomUserControl />
            </Expander>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemsSource>
        <Binding Path="DataToBeEditied" />
    </ListBox.ItemsSource>
</ListBox>

The custom user controls are a user control I created which edit the data from DataToBeEdited which is an ObservableCollection of data

According to what I can find there should be a way but no explanation on how. How Do I do it?

like image 982
Hampus Avatar asked Feb 01 '26 12:02

Hampus


1 Answers

You could define an ItemContainerStyle:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="BorderThickness" Value="2" />
            <Setter Property="BorderBrush" Value="Red" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
like image 193
mm8 Avatar answered Feb 04 '26 00:02

mm8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!