I have a bindable property in a custom control (HealthBar):
public static readonly BindableProperty ValueProperty = BindableProperty.Create(
nameof(Value),
typeof(int),
typeof(HealthBar),
0,
BindingMode.TwoWay,
propertyChanged: ValueChanged);
Is it possible to force the propertychanged (ValueChanged method) to fire even if I'm setting the same value to the property? The ValueChanged method is doing some calculation to set the width of the health bar.
private static void ValueChanged(BindableObject bindable, object oldValue, object newValue)
{
HealthBar obj = bindable as HealthBar;
if (obj != null)
{
obj.RecalculateWidth();
}
}
I know this sounds a bit mad, so here are some more details; I need to force the bar to recalculate the width as there are a few of these in a listview, and adding more to the ObservableCollection causes the widths to mess up. Values are being updated via signalR every 2 seconds so the correct width should show next time the Value property is set.
Here is the code that recalcuates the width for clarity:
private void RecalculateWidth()
{
double val = this.Value;
double max = this.Max;
double percent = (val / max) * 100;
double width = (double)this.Width * (double)percent / 100;
this.bar.Layout(new Rectangle(0, 0, width, this.Height));
}