Bind a child's property to a parent's in c#

BenVutiBenVuti USMember ✭✭
edited June 2017 in Xamarin.Forms

Hi,
I found TemplateBinding which allows me to bind a child's property to a parent's in c#.
The issue is that my child element is 6 steps bellow the Parent holding the property I want.
I can't seem to be able to reach it. Any solution ?

Tagged:

Best Answer

  • BenVutiBenVuti US ✭✭
    edited March 26 Accepted Answer

    After digging around for a while I decided to bypass the problem like this:
    My DetailsPage is a page designed to display any object following a set of rules.
    The simplest way that came to me in order to do that was to use a custom Layout

    class MetadataLayout : Layout<View> 
    {
        public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemSource", typeof(EntityMetadata), typeof(MetadataLayout), default(EntityMetadata), BindingMode.TwoWay, propertyChanged: OnItemsSourceChanged);
    }
    

    That way, I just had to do that in my XAML

    <controls:MetadataLayout ItemsSource="{Binding Metadata}"/>

    The issue was that some fields are linking to other pages and that all the Navigation logic is held by the Page's ViewModel

    So I tried playing with the bindings in c#, but got nowhere.

    The simplest thing to do was to pass the command by adding a binding to my custom layout

    class MetadataLayout : Layout<View> 
    {
        public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemSource", typeof(EntityMetadata), typeof(MetadataLayout), default(EntityMetadata), BindingMode.TwoWay, propertyChanged: OnItemsSourceChanged, propertyChanging: OnItemsSourceChanged);
    
        public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(MetadataLayout), default(ICommand));
    
        public EntityMetadata ItemsSource 
        {
            get => (EntityMetadata)GetValue(ItemsSourceProperty);
            set => SetValue(ItemsSourceProperty, value); 
        }
    
        public ICommand Command 
        {
            get => (ICommand)GetValue(CommandProperty);
            set => SetValue(CommandProperty, value);
        }
    

    <controls:MetadataLayout ItemsSource="{Binding Metadata}" Command="{Binding LinkTappedCommand}"/>

    Like that, I can pass the command wherever I want to down the object tree.

    I anyone know of a better solution to my maybe-flawed-design, please let me know :)

Answers

  • BenVutiBenVuti USMember ✭✭
    edited March 26 Accepted Answer

    After digging around for a while I decided to bypass the problem like this:
    My DetailsPage is a page designed to display any object following a set of rules.
    The simplest way that came to me in order to do that was to use a custom Layout

    class MetadataLayout : Layout<View> 
    {
        public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemSource", typeof(EntityMetadata), typeof(MetadataLayout), default(EntityMetadata), BindingMode.TwoWay, propertyChanged: OnItemsSourceChanged);
    }
    

    That way, I just had to do that in my XAML

    <controls:MetadataLayout ItemsSource="{Binding Metadata}"/>

    The issue was that some fields are linking to other pages and that all the Navigation logic is held by the Page's ViewModel

    So I tried playing with the bindings in c#, but got nowhere.

    The simplest thing to do was to pass the command by adding a binding to my custom layout

    class MetadataLayout : Layout<View> 
    {
        public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemSource", typeof(EntityMetadata), typeof(MetadataLayout), default(EntityMetadata), BindingMode.TwoWay, propertyChanged: OnItemsSourceChanged, propertyChanging: OnItemsSourceChanged);
    
        public static readonly BindableProperty CommandProperty = BindableProperty.Create("Command", typeof(ICommand), typeof(MetadataLayout), default(ICommand));
    
        public EntityMetadata ItemsSource 
        {
            get => (EntityMetadata)GetValue(ItemsSourceProperty);
            set => SetValue(ItemsSourceProperty, value); 
        }
    
        public ICommand Command 
        {
            get => (ICommand)GetValue(CommandProperty);
            set => SetValue(CommandProperty, value);
        }
    

    <controls:MetadataLayout ItemsSource="{Binding Metadata}" Command="{Binding LinkTappedCommand}"/>

    Like that, I can pass the command wherever I want to down the object tree.

    I anyone know of a better solution to my maybe-flawed-design, please let me know :)

Sign In or Register to comment.