Sturm
Sturm

Reputation: 4125

TextBlock binding format

How could I achieve this behavior in my ComboBox items: (n): Name

Where n and Name are two bindable properties. Right now I have n Name

This is my code:

<ComboBox.ItemTemplate>
    <DataTemplate>
        <TextBlock>
            <Run Text="{Binding n}" />
            <Run Text="{Binding Name}" />
        </TextBlock>
    </DataTemplate>
</ComboBox.ItemTemplate>

I think that adding two <Run Text="("/>, etc it can be done but there must be something more elegant in XAML.

Upvotes: 0

Views: 513

Answers (2)

user5109611
user5109611

Reputation:

Another way is using multiple TextBlocks like so:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Path=...}"></TextBlock>
        <TextBlock Text="{Binding Path=...}"></TextBlock>
    </StackPanel>
</DataTemplate>

You can replace the StackPanel with a WrapPanel or whatever you like.

Upvotes: 1

Sturm
Sturm

Reputation: 4125

Ok it was easy:

 <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock>
                            <TextBlock.Text>
                                <MultiBinding StringFormat="{}({0}): - {1}">
                                    <Binding Path="n" />
                                    <Binding Path="Name" />
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </DataTemplate>
                </ComboBox.ItemTemplate>

Upvotes: 1

Related Questions