Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Binding and/or Converter Not Working

$
0
0

Trying to make a custom view that contains a ListView control. This is a control I made in WinPhone XAML and trying to port to Xamarin.Forms XAML now. In the item data template there is a content control that I am trying to bind the currently selected list item to and compare it in the converter to the current template item so only the selected item shows its content. Essentially like an accordion control. The problem is the converter is never called... or the binding is incorrect as it is hard to debug Xamarin bindings I'm not real sure.

<ContentView
    x:Class="SH.XamarinForms.Controls.Accordion"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:localControls="clr-namespace:SH.XamarinForms.Controls;assembly=SH.XamarinForms"
    xmlns:localUtilities="clr-namespace:SH.XamarinForms.Utilities;assembly=SH.XamarinForms"
    x:Name="AccordionItemsControl">
  <Grid>
    <ListView x:Name="ItemListView"
              BindingContext="{x:Reference AccordionItemsControl}"
              ItemsSource="{Binding ItemsSource}">
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <Grid>
                <Grid.RowDefinitions>
                  <RowDefinition Height="Auto" />
                  <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.Resources>
                  <ResourceDictionary>
                    <localUtilities:EqualityToBooleanConverter x:Key="EqualityToBooleanConverter" Item="{Binding}" />
                  </ResourceDictionary>
                </Grid.Resources>
                <localControls:ContentControl Grid.Row="0" ContentTemplate="{Binding HeaderTemplate, Source={localUtilities:RootSource}}" />
                <localControls:ContentControl Grid.Row="1" x:Name="ItemContentControl" 
                                              ContentTemplate="{Binding ItemTemplate, Source={localUtilities:RootSource}}}" 
                                              IsVisible="{Binding ItemListView.SelectedItem, Source={localUtilities:RootSource}, Converter={StaticResource EqualityToBooleanConverter}}" /><!-- IsVisible Binding Not Working -->
              </Grid>
            </ViewCell>
          </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
  </Grid>
</ContentView>

namespace SH.XamarinForms.Utilities
{
    public class RootSource : IMarkupExtension
    {
        public object ProvideValue(IServiceProvider serviceProvider)
        {
            var rootProvider = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
            if (rootProvider == null)
                return null;
            var root = rootProvider.RootObject as Element;
            return root;
        }
    }
}

namespace SH.XamarinForms.Utilities
 {
 public class EqualityToBooleanConverter : BindableObject, IValueConverter
 {
 //Properties - Dependency
 public static readonly BindableProperty ItemProperty = BindableProperty.Create("Item", typeof(Object), typeof(EqualityToBooleanConverter), null);
 public Object Item
 {
 get { return (Object)GetValue(ItemProperty); }
 set { SetValue(ItemProperty, value); }
 }
    //Methods
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == Item)
            return true;
        return false;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
}

Any ideas why that binding and/or converter is not working? If I strip it down to something like IsVisible="{Binding Converter={StaticResource EqualityToBooleanConverter}}" then the converter is called. Tried making a BindableProperty in the code behind to store the selected item and using that in the binding, but same results. All the other bindings in the above example work fine.

Any help in getting past this binding issue would be great. Thanks a lot!


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>