Hello,
Welcome to our Microsoft Q&A platform!
We can try to handle data in the property changed event of bindable property instead of the setter method of a normal property.
So ,we can use the following code instead :
public class Circle: Frame
{
public static readonly BindableProperty RadiusProperty = BindableProperty.Create(nameof(Radius), typeof(double), typeof(Circle), 125.0, BindingMode.TwoWay, propertyChanged: RadiusChanged);
public double Radius
{
get => (double)GetValue(RadiusProperty); //_radius;
set => SetValue(RadiusProperty, value);
}
static void RadiusChanged(BindableObject bindableObject, object oldValue, object newValue)
{
Circle circle = bindableObject as Circle;
circle.HeightRequest = (double)newValue;
circle.WidthRequest = (double)newValue;
circle.CornerRadius = (float)((double)newValue / 2);
}
}
And use like this:
<controls:Circle Radius=" 70" BackgroundColor="Green"/>
In addition, if you want to use parent page's bindable property as the Circle's binding context, you can do like this:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:XamarinFormsPlayground.Controls;assembly=XamarinFormsPlayground"
x:Class="XamarinFormsPlayground.MainPage"
x:Name="myPage"
>
<ContentPage.Content>
<StackLayout>
<controls:Circle Radius=" 70" BackgroundColor="Green"/>
<controls:Circle BackgroundColor="{Binding InnerColor, Source={x:Reference myPage}}" Radius="{Binding InnerRadius, Source={x:Reference myPage}}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
Note: myPage
is property x:Name
of current page.
Best Regards,
Jessie Zhang
---
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.