开发者

Binding as a formatted string

开发者 https://www.devze.com 2023-03-22 13:29 出处:网络
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 To

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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消