13
votes

I have a Textblock that is bound to a property in the ItemsSource collection. I'd like to display two properties from that class in the same textblock, but it seems I can only perform one binding at a time. I currently have this:

Text="{Binding Title}"

but I want I want to append another property, so in theory it would be:

Text="{Binding Title - Author}" 

with the output looking like "Shakespeare - Romeo and Juliet". I've tried adding a comma, another binding and other things, but they all result in an exception being thrown (e.g. Unknown attribute Text on element TextBlock).

Both properties comes from the same class, so I won't need to have two data sources.

8
Are you using the MVVM pattern? If so, just create another property on your view model that returns the string in the way you wish.Chris Dunaway
If you were using a ViewModel from the MVVM pattern, you could just add a property to meet your needs. By the way, why not use 2-3 texblocks?agent-j
Not using MVVM. I tried using multiple textblocks, but I needed to position the text next to each other and some text was longer than others, so it would be tricky to lines them all up perfectly.Skoder

8 Answers

8
votes

Unfortunately silverlight is missing a few pieces that WPF has that can handle this. I would probably go the route of using a value converter that you can pass the class that contains the Title and Author to format the text.

Here's the code:

public class TitleAuthorConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (!(value is Book)) throw new NotSupportedException();
        Book b = value as Book;
        return b.Title + " - " + b.Author;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
}

And some XAML:

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.Resources>
        <local:Book Title="Some Book" Author="Some Author" x:Key="MyBook"/>
        <local:TitleAuthorConverter x:Key="Converter"/>
    </Grid.Resources>
    <TextBlock DataContext="{StaticResource MyBook}" Text="{Binding Converter={StaticResource Converter}}"/>
</Grid>

The downside to this way is that you won't get the text to update if the properties change (ie you implement INotifyPropertyChanged) since the field is bound to the class.

As suggested in the comments on the question, you could also create a third property that combines them. This would get around having to use multibindings or value converters.

18
votes

Use this one.. It will work Perfectly.

<TextBlock>
    <Run Text="{Binding Title}"></Run>
    <Run Text=":"></Run>
    <Run Text="{Binding Author}"></Run> 
</TextBlock>

the output will be like this,

OOPS:Balagurusamy

4
votes

Sounds like you need a MultiBinding.

<TextBlock.Text>
    <MultiBinding StringFormat="{}{0} - {1}">
        <Binding Path="Title" />
        <Binding Path="Author" />
    </MultiBinding>
</TextBlock.Text>
1
votes

You can use special extension from CodeProject

Example

<TextBlock>
<TextBlock.Text>
    <MultiBinding StringFormat="{}{0} {1}" >
        <Binding Path="FirstName" />
        <Binding Path="LastName" />
    </MultiBinding>
</TextBlock.Text>
</TextBlock>
0
votes

I found a MultiBinding sample project for Silverlight on the net, there are a number of them I believe, just Google it. If you can't find one let me know I'll send you the one we're using. Does anyone know if multi-binding is coming to SL 5? You can use a 3rd property as well, just remember to fire it's PropertyChanged event when ever either of it's constituents changes then binding will be fine.

0
votes
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
    <Binding Path="Title" />
    <Binding Path="Author" />
</MultiBinding>

0
votes

If the Text you want display it in the UI is read only, You can try overriding ToString of your view model object and return the combined value.

return Titile + ":" + Author;

0
votes

A very simple solution would be having a property in your ViewModel like this:

        public string TextblockDataProvider
    {
        get
        {
            return string.Format("{0} - {1}", Title, Author);
        }
        set { ; }
    }

and bind in xaml:

Text="{Binding TextblockDataProvider}"