rut ray
rut ray

Reputation: 33

Add multi-line text inside ListBox

I have this ListBox:

<ListBox Name="lbAFiles" HorizontalAlignment="Left" Height="100" Margin="318,61,0,0" 
         VerticalAlignment="Top" Width="481">
    <ListBoxItem>
        <TextBlock>Line1<LineBreak/>Line2</TextBlock>
    </ListBoxItem>
</ListBox>

I have my own object with several properties that i want to fill inside my ListBox instead of the line:

<TextBlock>Line1<LineBreak/>Line2</TextBlock>

How can i do that with code behind ?

Upvotes: 1

Views: 463

Answers (1)

karfus
karfus

Reputation: 939

        lbAFiles.Items.Add("Line 1");
        lbAFiles.Items.Add("Line 2");

That said, it would be better to use MVVM and a ViewModel, but you asked how to do it in code behind.

Whatever the object you want to add there, you could pull the properties into the string you're adding.

Another (better) option would be to add this object directly to the ListBox, and use a DataTemplate to give it a "look" (otherwise you will just get the ToString value of that object).

And a much better option is to use data binding.

Upvotes: 1

Related Questions