locked
Binding issue in UWP project of an Xamarin.Forms app RRS feed

  • Question

  • User42522 posted

    My ItemsVM is:

        public class ItemsVM : BaseViewModel
        {
            public ObservableCollection<Item> Items { get; set; }
            … … ...
        }
    

    Binding in a ContentPage is done as follows:

        public partial class MyPage : ContentPage
        {
            pageItemsVM ItemsVM;
    
            public MyPage()
            {
                InitializeComponent();
    
                BindingContext = pageItemsVM = new ItemsVM();
            }
    
            … … ...
        }
    

    Bindings to ListView are as follows:

    <ListView ItemsSource="{Binding Items}">
        … … …
                                                <StackLayout Margin="-2,10,-2,0"
                                                             BackgroundColor="{Binding Path=., Converter={StaticResource ConvertItemType2ColorBg}}"
                                                             HorizontalOptions="FillAndExpand">
                                                   … … ...
                                                </StackLayout>
        … … ...
    </ListView>
    

    The converter code is:

        public class ConvertItemType2ColorBg: IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                Item item = (Item)value;
                Color bgColor = Color.LightGreen;
    
                … … ...
                return bgColor;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    

    In Android & iOS, in converter the value could be cast to Item. But in UWP the value passed is of type ItemsVM and not Item.

    The page is bound to ItemsVM and the ListView is bound to ItemsVM.Items. Then the row should be bound to Item. Then how can this happen? How do I resolve it?

    Wednesday, April 29, 2020 1:48 PM

Answers

  • User369979 posted

    Try this code to exclude unnecessary data:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // default background color
        Color bgColor = Color.Blue;
        if (value is Item)
        {
            Item item = (Item)value;
    
            bgColor = Color.LightGreen;
        }
    
    
        return bgColor;
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 30, 2020 3:37 AM

All replies

  • User369979 posted

    Try this code to exclude unnecessary data:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // default background color
        Color bgColor = Color.Blue;
        if (value is Item)
        {
            Item item = (Item)value;
    
            bgColor = Color.LightGreen;
        }
    
    
        return bgColor;
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, April 30, 2020 3:37 AM
  • User42522 posted

    @LandLu said: Try this code to exclude unnecessary data:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // default background color
        Color bgColor = Color.Blue;
        if (value is Item)
        {
            Item item = (Item)value;
    
            bgColor = Color.LightGreen;
        }
    
    
        return bgColor;
    }
    

    ThanQ...That resolved my issue. I have set it in all converters of similar structure.

    But I don't understand why that occurs in UWP only and not in Android and iOS. I know that in Android and iOS the Converters are called multiple times but it was passing a null Object for other times. But in UWP it was passing the parent object.

    Thursday, April 30, 2020 2:38 PM