BindingMode=TwoWay in codebehind

Viewed 509

Edit: apologies for the bad title, apparently something like "Setting bindingmode in code behind" didn't fit the highly nebulous requirements of SO. A title that is clearly more obvious than what the current one is.

Original: I am trying to set the binding of my listview in the code behind of its data selector template. The reason i am doing this, as I suspect (because doing something similar to it in a different template selector fixed it) that once you exit that page android seems to still contain a reference to it and then throws a amarin.forms.platform.android.viewcellrendererA disposed object exception.

my current xaml looks as follows:

<StackLayout Orientation="Vertical" 
                             Padding="0, 20, 0, 0"
                             HorizontalOptions="FillAndExpand"
                             VerticalOptions="StartAndExpand">

        <Label 
            x:Name="LabelName"
            FontAttributes="Bold"
            TextColor="Black"
            FontSize="14"
            VerticalOptions="Start"
            HorizontalOptions="FillAndExpand"/>


        <ListView x:Name="MultiselectList"
               SeparatorVisibility="None"
               RowHeight="30"
               Margin="0, 10, 0, 0"
               VerticalOptions="FillAndExpand"
               HorizontalOptions="StartAndExpand"                 
               SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <customcontrols:NoHighlightCell>
                        <StackLayout Orientation="Horizontal" 
                                         HorizontalOptions="FillAndExpand"
                                         VerticalOptions="StartAndExpand">

                            <Image 
                                x:Name="image"
                                Source="{Binding ImageUrl}"  
                                VerticalOptions="StartAndExpand"
                                HorizontalOptions="Start"
                                Margin="0, 2, 0, 0"
                                WidthRequest="15" 
                                HeightRequest="15" />

                            <Label        
                                x:Name="name"
                                Text="{Binding Name}"  
                                TextColor="Black"
                                VerticalOptions="StartAndExpand"
                                FontSize="14"
                                HorizontalOptions="Start"/>
                        </StackLayout>
                    </customcontrols:NoHighlightCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>


        <Label Text="Selection required"
                   TextColor="Red"
                   Margin="0, 10, 0, 0"
                   IsVisible="{Binding ValidationRequired}"
                   VerticalOptions="StartAndExpand"
                   HorizontalOptions="FillAndExpand"
                   FontSize="14"/>

    </StackLayout>

Here's my code behind:

private SoapNoteControlsViewModel viewModel;

        public SelectListTemplate()
        {
            InitializeComponent();
        }

        protected override void OnDisappearing()
        {
            base.OnDisappearing();
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            LabelName.Text = viewModel.Label;

            MultiselectList.ItemsSource = viewModel.CheckboxItems;
            MultiselectList.SelectedItem = viewModel.SelectedItem;
            //how would i set the BindingMode here? Doing .SetBinding doesn't seem to do it.

        }

        protected override void OnBindingContextChanged()
        {
            try
            {
                base.OnBindingContextChanged();

                if(BindingContext == null)
                {
                    return;
                }

                viewModel = BindingContext as SoapNoteControlsViewModel;

            } catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }

I tried doing .SetBinding, but it keeps saying that BindingMode is a type (given that it is an enum). Looking at some of the examples on MSDN haven't really helped

1 Answers

You would do it like this:

MultiselectList.SetBinding(ListView.ItemsSourceProperty, nameof(viewModels.CheckboxItems), BindingMode.TwoWay);
  1. The first parameter is the property that you want to bind to - e.g. the ItemsSource.
  2. The second parameter is the name of the property to look for - e.g. your viewModel's CheckBoxItems. This collection will be bound/populated in the list.
  3. The last parameter is what you would want to achieve - the BindingMode. You are correct in saying that it is an enum, so here we can set it to BindingMode.TwoWay.

For reference: BindingMode's Remarks page.

Related