S-Vuk
S-Vuk

Reputation: 369

Multiline text in WPF ListView

I have a WPF ListView that I need to populate with some text data. I would like this text to be on multiple lines in each ListView row.

Example:

Person 1 | Address
Age:     | Address2

Person 2 | Address
Age:     | Address2

Etc.

I would like to also format the text, and for my purposes this would all be in a one-column ListView.

If multiline is not possible, is there a better control for displaying a vertical list of binded data that allows for selection as well?

Upvotes: 2

Views: 3741

Answers (1)

bdimag
bdimag

Reputation: 963

You can set the ListBox's ItemTemplate to achieve something like that

<ListView ItemsSource="{Binding People}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <StackPanel>
                    <TextBlock Text="{Binding Name}" />
                    <TextBlock Text="{Binding Age, StringFormat='Age: {0}'}" />
                </StackPanel>
                <StackPanel Grid.Column="1">
                    <TextBlock Text="{Binding Address1}" />
                    <TextBlock Text="{Binding Address2}" />
                </StackPanel>
            </Grid>                    
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Upvotes: 4

Related Questions