1
votes

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.

1
Your assertion that it will call the ToString method just once, because it never receives a PropertyChange notification that would cause it to recall it. I think you can force it to call ToString again by doing a PropertyChanged(null) which tells it to check all bindings again. Beyond that, I don't understand the rest of your question.CodingGorilla
@CodingGorilla: This won't do since there is no binding to begin with, the method just gets executed and that't it.H.B.
Yea, that's why I said "I think...", I wasn't sure but thought it might be worth a try. Seems like for some reason he can't or doesn't want to bind to a proper property.CodingGorilla
What i want to do is show two properties instead of just one.Rushino

1 Answers

3
votes

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>