How to set a control as a property in other control in XAML in Xamarin Forms?


This Content is from Stack Overflow. Question asked by Junaid Pathan

I want to set a control as a property in XAML.

I have a custom Control BlurredImage that sends another control (XAML view or Visual Element) as the Root for generating blurred background for the current Image. I have implemented custom control.

Here’s my custom control:

public class BlurredImage : Image
    public static readonly BindableProperty RootProperty = BindableProperty.Create(nameof(Root), typeof(VisualElement), typeof(BlurredImage), propertyChanged: RootPropertyChanged);

    public VisualElement Root { get => (VisualElement)GetValue(RootProperty); set => SetValue(RootProperty, value); }

    static void RootPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        var control = (BlurredImage)bindable;

        Task.Run(async () =>
            var imageStream = await DependencyService.Get<IVisualElementExtension>().PlatformCaptureImageAsync(control.Root, Color.Red);
            control.Source = ImageSource.FromStream(() => imageStream);

The RootPropertyChanged() is added here for reference, it works properly when capturing image of a UI by a button click, it only has problem when I specify Root="{Binding someView, Source={x:Reference this}}"

Here is my XAML code when I am creating BlurredImage in my XAML Page:

<ext:BlurredImage Root="{Binding ssView, Source={x:Reference this}}" HeightRequest="200" WidthRequest="200" />

Am I doing everything correctly? How can I pass a control as a property value from XAML?


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?