Reputation: 3641
I have a listbox and I want to show two properties from my object.
For one property it is pretty easy:
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name} "></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
However I would like something like
Name - GroupName
not just Name...
I know that it is easy, but I am confused. I don't know how to add two textblocks into the same level of the XAML hierarchy.
Upvotes: 0
Views: 330
Reputation: 7517
You can do this a couple of ways. One is with a MultiBinding
and a StringFormat
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding Path="Name" />
<Binding Path="GroupName" />
</MutliBinding>
</TextBlock.Text>
</TextBlock>
The second is option is to set the root element of your DataTemplate
to something that can have more than one child, like a StackPanel
and have to TextBlock
controls each bound to the appropriate value.
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text=" - " />
<TextBlock Text="{Binding Path=GroupName}" />
</StackPanel>
</DataTemplate>
I prefer the first option.
Upvotes: 1