Home > OS >  Xamarin - bind custom view content
Xamarin - bind custom view content

Time:01-06

I have created a custom view and I would like to set its content by binding. This is what I have:

    <ContentView.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="30"/>
            </Grid.ColumnDefinitions>

            <ffimageloadingsvg:SvgCachedImage Source="resource://CTP.SVG.exit.svg"
                                      Grid.Row="0" Grid.Column="1"
                                      HeightRequest="30" 
                                      WidthRequest="30"
                                      HorizontalOptions="Center"
                                      VerticalOptions="Center">
            </ffimageloadingsvg:SvgCachedImage>

            <redefinitions:BindableView Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" 
                                        Content="{Binding View, Source={x:Reference this}, Mode=OneWay}"/>

        </Grid>
    </ContentView.Content>

And following this tutorial I have defined my BindableView:

[ContentProperty(nameof(Content))]
public class BindableView : View
{
    public static readonly BindableProperty ContentProperty =
    BindableProperty.Create(
        propertyName: nameof(Content),
        returnType: typeof(View),
        declaringType: typeof(BindableView),
        defaultValue: default(View));

    public View Content
    {
        get { return (View)GetValue(ContentProperty); }
        set { SetValue(ContentProperty, value); }
    }
}

But the BindableView is never showing, even if I define its Content statically. What am I missing?

CodePudding user response:

Easiest way to solve it is to use ContentView instead the custom BindableView.

  •  Tags:  
  • Related