Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How can I pass reference to control/value of control into my custom control?

LeloLelo Member ✭✭✭

I have a control that takes an Entry in constructor

public AutocompleteCollectionView(Entry entry)
        {
            InitializeComponent();
            this.Layout(new Rectangle(new Point(entry.X, entry.Height), new Size(entry.Width, 200)));

            collectionView.BindingContextChanged += (sender, e) => OnBindingContextChanged(sender, e);
        }

Since x:arguments is used to pass arguments to to the constructor I have tried these two approaches:

<Entry x:Name="entry" Placeholder="I am placeholder"/>
    <controls:AutocompleteCollectionView>
        <x:Arguments>
            <x:entry />
        </x:Arguments>
    </controls:AutocompleteCollectionView>

And:

<controls:AutocompleteCollectionView>
        <x:Arguments>
            <Entry Placeholder="I am placeholder"/>
        </x:Arguments>
    </controls:AutocompleteCollectionView>

With the second approach I receive the entry successfully in the AutocompleteCollectionView. However the entry won't be displayed. Note that the only thing am actually being interested in is the bounds - but it would also be handy to have access to the whole object since I might implement TextChanged of the entry inside AutocompleteCollectionView. How can I have the entry being displayed on the ContentPage like normal while passing the reference to it to my custom AutocompleteCollectionView control?

Best Answers

Answers

  • LeloLelo Member ✭✭✭

    I made a BindableProperty like this

    public static readonly BindableProperty EntryProperty =
                BindableProperty.Create("Entry", typeof(Entry), typeof(AutocompleteCollectionView), default(Entry));
    
            public Entry Entry
            {
                get { return (Entry)GetValue(EntryProperty); }
                set { SetValue(EntryProperty, value); }
            }
    

    But I am now struggling with passing the entry

    <Entry x:Name="entry" Placeholder="I am placeholder" />
                <controls:AutocompleteCollectionView Entry="{Binding entry}">
    
                </controls:AutocompleteCollectionView>
    
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    edited May 2019

    Naming your Entry as Entry is probably not the best practice. It causies confusion. Maybe EntryBox. Then you know what it is, know when you are referring to the class versus the type, etc.

    The fact that you're passing a UI element to it at all is odd. Why aren't you just inheriting from Entry to begin with?

  • LeloLelo Member ✭✭✭

    I set it up like this inside my custom AutocompleteCollectionView control.

    public static readonly BindableProperty EntryProperty =
                BindableProperty.Create("Entry", typeof(Entry), typeof(AutocompleteCollectionView), default(Entry), propertyChanged: EntryPropertyChanged);
    

    And then I had to use x:reference to have the entry to be received in my EntryPropertyChanged method (as newValue)

    <Entry x:Name="entry" Placeholder="I am placeholder" />
                <controls:AutocompleteCollectionView Entry="{x:Reference entry}">
                </controls:AutocompleteCollectionView>
    
Sign In or Register to comment.