0
votes

I have a bunch of key/value pair text data that I need to display.

I currently use LongListSelector with an ItemTemplate which uses a horizontal StackPanel with two TextBlock s inside:

<StackPanel Orientation="Horizontal">
    <TextBlock Text="Title: " />
    <TextBlock Text="{Binding Path=Title}" TextWrapping="Wrap" />
</StackPanel>

This gives a nice "label: value" look.

However, I find this verbose. Is there a nicer way to achieve the same thing?

I've tried putting everything in a single TextBlock like so:

<TextBlock Text="Title: {Binding Path=Title}" TextWrapping="Wrap" />

but it doesn't want to work!

1

1 Answers

2
votes

Inside a single TextBlock, this is not possible. However, you could use a ValueConverter:

public class NameValueConverter : IValueConverter
{
    public object Convert(Object value,
    Type targetType,
    Object parameter,
    CultureInfo culture)
    {
        return "Title: " + (string)value;
    }
}

You should add this to your app.xaml as a global resource (named NameValueConverter). Then you can do this in XAML:

<TextBlock Text="{Binding Path=Title, Converter={StaticResource NameValueConverter}}" TextWrapping="Wrap" />

When databinding kicks in, it will pass the Title to the valueconverter and bind the result to the textbox.