Rushino
Rushino

Reputation: 9505

Binding as a formatted string

I have a ListBox which hold a set of objects (linked via ItemsSource bind to an ObservableCollection). I haven't used Dynamic binding yet. It currently use the ToString() method of the object. The ToString() method shows a string this way : name (someOtherProperty)

However, even if the INotifyPropertyChanged is implemented and that i use an ObservableCollection, if i change an item property this string won't be updated.

I believe that this is because it only calls ToString once. instead i guess i have to use data binding but how i can form such a string with it ? << name (someOtherProperty) >>

Thanks.

Upvotes: 1

Views: 144

Answers (1)

brunnerh
brunnerh

Reputation: 185445

You can use a multibinding, e.g. something like this:

<MultiBinding StringFormat="{}{0} ({1})">
    <Binding Path="name"/>
    <Binding Path="someOtherProperty"/>
</MultiBinding>

If you just let it execute ToString there is no proper binding at all, any notifications will have no effect.

You use it like this:

<ListBox ...>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <!-- The above binding here -->
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Upvotes: 3

Related Questions