开发者

WPF databinding and converters

开发者 https://www.devze.com 2022-12-08 22:18 出处:网络
I\'m trying to databind to a listbox like so: <ListBox x:Name=\"MyListBox\" Margin=\"0,0,0,65\">

I'm trying to databind to a listbox like so:

<ListBox x:Name="MyListBox" Margin="0,0,0,65">
    <ListBox.ItemTemplate>
       开发者_开发百科 <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource MyConverter}}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The reason I am binding to the whole object and not a property is because my converter will need multiple properties of the object to build the string that it returns.

This works and my string is returned. But then when I change the ObservableCollection that this is based on the value doesn't change on the screen. If I bind to just a single property and change it, then the value does change.

What can I do differently? I can't bind to a single property since I need the entire object in the converter... And the ConverterParameter is already being used.


Remember, if you bind to the "main" property and the value of the main property itself isn't changed, the binding will have no reason to refresh itself. It has no clue that your converter is actually based off of a sub-property. What you can do is use a MultiBinding where you bind not only the "main" property, but also a specific sub-property. This gives your IMultiValueConverter implementation access to the main data object, but because you're also binding to the sub-property that's changing, will also be refreshed when that sub-property's value changes.


You can try using a MultiBinding which I believe updates whenever any of its Bindings are triggered. You can also use an IMultiValueConverter or just take advantage of the StringFormat of the binding.

0

精彩评论

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

关注公众号