Reputation: 4053
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.
Upvotes: 13
Views: 14305
Reputation: 621
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>
Upvotes: 1
Reputation: 127
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}"
Upvotes: 0
Reputation: 37
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="Title" />
<Binding Path="Author" />
</MultiBinding>
Upvotes: 0
Reputation: 837
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
Upvotes: 18
Reputation: 748
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.
Upvotes: 0
Reputation: 5445
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.
Upvotes: 8
Reputation: 26883
Sounds like you need a MultiBinding.
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="Title" />
<Binding Path="Author" />
</MultiBinding>
</TextBlock.Text>
Upvotes: 6