开发者

WP7 LonglistSelector databinding - How to trigger an update to a bound item's binding

开发者 https://www.devze.com 2023-03-20 19:33 出处:网络
The longlistselector control appears to be just what I need for my application as I have a very long list that I need to display and I like the grouping features that it implements, but I\'m really st

The longlistselector control appears to be just what I need for my application as I have a very long list that I need to display and I like the grouping features that it implements, but I'm really struggling with the databinding aspect of working with it. It took a while to get the basic databinding working, but I have that fully functional. If an item is removed or added the UI updates properly because the datasource derives from ObservableCollection.

The problem that I am having is that I have a flag in the individual dataitems that I am binding to that indicates if an individual list item should display or hide a graphic. The idea is that when the user performs a hold gesture on an item it will toggle the graphic on or off based on changes to the bound item's properties.

    Visibility="{Binding Converter={StaticResource isFavoriteToVisibility}}

The LongListSelector databinding requirements for getting the group headers and such requires a datasource like ObservableCollection<ObservableCollection<MyItem>> (which is confusing just to look at!). Essentially, the outer collection is the Groups and the the inner collection contains the displayed items. My items even implement the INotifyPropertyChanged interface. What I'd expected was for updates to my item's properties to be automatically reflected in my databinding, not just the addition or removal of items from the collection.

If I toggle the property value nothing whatsoever happens until I manually refresh the binding which requires a full reload. It is possible that this control doesn't respond the propertychanged events of the individual items.

What I need is help figuring out what I can do to trigger an update of an individual list item after I have changed a property of it's bound object?

Update:

Claus, the code that you created was VERY helpful (sorry, I don't have enough rep to vote you up yet!) Using that code I was able to significantly simplify my code, and do more tests, but it still doesn't solve my problem.

What I've now determined is that my binding is fine. If I change the property when bound simply to a text field text="{Binding IsFavorite}" it updates flawlessly. What doesn't seem to work is when I update that same property when bound to the IValueConverter Visibility="{Binding Converter={StaticResource isFavoriteToVisibility}}" it doesn't update. The converter works fine at initial binding, but not on property change. To go farther with testing, I bound the same bool property to both a text field and the IValueConverter. When I change the bool value the text field updates, but not the field bound using the converter.

public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Chemical chem = value as Chemical;

        if (chem == null)
            return Visibility.Collapsed;

        if (chem.IsFavorite)
            return Visibility.Visible;
        else
            return Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML:

<myNS:ChemicalToFavoriteVisibilityConverter x:Key="isFavoriteToVisibility" />

<Rectangle Grid.Column="0" 
           Fill="{StaticResource PhoneContrastBackgroundBrush}" 
           Height="26" Stroke="Black"  Width="26" 
           Visibility="{Binding Converter={StaticResource isFavoriteToVisibility}}">
    <Rectangle.OpacityMask>
        <ImageBrush ImageSource="/Images/star_big.png"/>
    </Rectangle.OpacityMask>
</Rectangle>
<StackPanel Grid.Column="1"  Margin="0,0,0,0">
    <TextBlock Text="{Binding IsFavorite}" 
               TextWrapping="Wrap" 
               Style="{StaticResource Pho开发者_如何学PythonneTextLargeStyle}" 
               Foreground="{StaticResource PhoneForegroundBrush}"/>
</StackPanel>


I solved the problem. The problem was that I was passing the entire object to the IValueConverter instead of the specific property that changed within the object. I changed the converter to specifically handle conversion of bool to Visibility and fixed the binding:

public class BoolToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool boolValue = (bool)value;

        return boolValue ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

The binding changed from:
Visibility="{Binding Converter={StaticResource isFavoriteToVisibility}}"

to:
Visibility="{Binding Converter={StaticResource isFavoriteToVisibility}, Path=IsFavorite}"


Can you verify that in your binding you are not using OneTime as the Binding Mode (instead of OneWay)?

I've used data binding without problems in items in a list bound to the LongListSelector - I suspect that the issue won't lie with the LongListSelector, but with your binding or your INotifyPropertyChanged ... can you post some code (ideally the binding, and also the objects being bound).

0

精彩评论

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

关注公众号