0

I have a view of a listView which in turn contains an ItemTemplateSelector. In one of the ItemTemplateSelector I have a ContentView created programmatically that has a BindableProperty that I bind when I'm creating the DataTemplate, but it doesn't load the value that I bring in my ViewModel. I have seen that in the ContentView in the XAML an x:name="this" is added and then in the container a bindingContext={x:Reference this} is done but how to do that programmatically?

The issue is that the binding is not working from the contentview to the parent viewmodel through the bindiable property. Any input is much appreciated.

  1. View
public class QuestionWithSelectorView : ContentPage
{
    public QuestionWithSelectorView(QuestionsViewModel vm)
    {
        BindingContext = vm;
        this.Title = "Questions";

        var grid = new Grid();
        grid.RowDefinitions = Rows.Define(GridLength.Star, GridLength.Auto);

        var radListView = new RadListView();
        radListView.ItemsSource = vm.Questions;
        radListView.ItemTemplateSelector = new QuestionTypeTemplateSelector();


        grid.SetRow(radListView, 0);
        grid.Children.Add(radListView);

        Content = grid;
    }
}
  1. Create DataTemplate
private static DataTemplate CreateMultipleOptionTemplate()
        {
            return new DataTemplate(() =>
            {
                var grid = new Grid();
                grid.RowDefinitions = Rows.Define(GridLength.Auto, GridLength.Auto, GridLength.Auto, GridLength.Star);

                var bindingQuestionName = new Binding(nameof(Question.QuestionName), BindingMode.Default);
                var bindingQuestionNumber = new Binding(nameof(Question.QuestionNumber), BindingMode.Default);

                var label = new Label();
                label.SetBinding(Label.TextProperty, bindingQuestionNumber);
                label.HorizontalOptions = LayoutOptions.Start;
                label.VerticalOptions = LayoutOptions.Center;
                grid.SetRow(label, 0);
                grid.Children.Add(label);

                var label2 = new Label();
                label2.SetBinding(Label.TextProperty, bindingQuestionName);
                label2.HorizontalOptions = LayoutOptions.Start;
                label2.VerticalOptions = LayoutOptions.Center;
                grid.SetRow(label2, 1);
                grid.Children.Add(label2);

                var bindingOptionsNumber = new Binding(nameof(Question.QuestionOptionNumber), BindingMode.TwoWay);

                var label3 = new Label();
                label3.SetBinding(Label.TextProperty, bindingOptionsNumber);
                label3.HorizontalOptions = LayoutOptions.Start;
                label3.VerticalOptions = LayoutOptions.Center;
                grid.SetRow(label3, 2);
                grid.Children.Add(label3);

                var bindingOptionsNumber2 = new Binding(nameof(Question.QuestionOptionNumber), BindingMode.TwoWay);
                //source: new RelativeBindingSource(RelativeBindingSourceMode.FindAncestorBindingContext,
                //        typeof(QuestionsViewModel)));
                var multipleOptionView = new MultipleOptionView();
                multipleOptionView.SetBinding(MultipleOptionView.OptionsNumberProperty, bindingOptionsNumber2);

                grid.SetRow(multipleOptionView, 3);
                grid.Children.Add(multipleOptionView);

                return new ListViewTemplateCell { View = grid };
            });
        }
  1. ContentView
public class MultipleOptionView : ContentView
{
    public static readonly BindableProperty OptionsNumberProperty = BindableProperty.Create(nameof(OptionsNumber), typeof(int), typeof(MultipleOptionView), 9);
    public MultipleOptionView()
    {
        
    }

    public int OptionsNumber { get; set; }

    void CreateView()...
   

    protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        base.OnPropertyChanged(propertyName);
        if (propertyName != null)
        {
            if (propertyName == "OptionsNumber")
            {
                if (OptionsNumber != 0)
                {
                    CreateView();
                }
            }
        }
    }
}

I tried adding source to the binding with a RelativeBindingSourceMode, but it didn't work

Sanushi Salgado
  • 1,195
  • 1
  • 11
  • 18
vordonez
  • 5
  • 1
  • MAUI and XF are two different frameworks, please choose the appropriate tag – Jason Jun 12 '23 at 23:28
  • Does this answer your question? You can refer to [.NET Maui Binding a contentview to parent ViewModel MVVM](https://stackoverflow.com/questions/75466143/net-maui-binding-a-contentview-to-parent-viewmodel-mvvm). – Guangyu Bai - MSFT Jun 16 '23 at 07:25

0 Answers0