I've a custom control that should change the background color of an entry when it's focused or unfocused. This works like a charm but I would like to have the colors that I've set in the xaml on the initial load and not only after trigger focus/unfocus. How do I do that or what is a good aproach?
Thanks in advance!
Here is my class:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SingleDigitEntry
{
public SingleDigitEntry()
{
InitializeComponent();
}
private static readonly BindableProperty BorderColorActiveProperty =
BindableProperty.Create(
nameof(BorderColorActive),
typeof(Color),
typeof(SingleDigitEntry),
Color.Blue);
public Color BorderColorActive
{
get => (Color)GetValue(BorderColorActiveProperty);
set => SetValue(BorderColorActiveProperty, value);
}
private static readonly BindableProperty BorderColorInActiveProperty =
BindableProperty.Create(
nameof(BorderColorInactive),
typeof(Color),
typeof(SingleDigitEntry),
Color.DarkBlue);
public Color BorderColorInactive
{
get => (Color)GetValue(BorderColorInActiveProperty);
set => SetValue(BorderColorInActiveProperty, value);
}
private static readonly BindableProperty BackgroundColorActiveProperty =
BindableProperty.Create(
nameof(BackgroundColorActive),
typeof(Color),
typeof(SingleDigitEntry),
Color.Blue);
public Color BackgroundColorActive
{
get => (Color)GetValue(BackgroundColorActiveProperty);
set => SetValue(BackgroundColorActiveProperty, value);
}
private static readonly BindableProperty BackgroundColorInactiveProperty =
BindableProperty.Create(
nameof(BackgroundColorInactive),
typeof(Color),
typeof(SingleDigitEntry),
Color.DarkBlue);
public Color BackgroundColorInactive
{
get => (Color)GetValue(BackgroundColorInactiveProperty);
set => SetValue(BackgroundColorInactiveProperty, value);
}
private static readonly BindableProperty BorderWithProperty =
BindableProperty.Create(
nameof(BorderWith),
typeof(int),
typeof(SingleDigitEntry),
0);
public int BorderWith
{
get => (int)GetValue(BorderWithProperty);
set => SetValue(BorderWithProperty, value);
}
private static readonly BindableProperty PreviousEntryProperty =
BindableProperty.Create(
nameof(PreviousEntry),
typeof(ExtendedEntry),
typeof(SingleDigitEntry),
null);
public ExtendedEntry PreviousEntry
{
get => (ExtendedEntry)GetValue(PreviousEntryProperty);
set => SetValue(PreviousEntryProperty, value);
}
private static readonly BindableProperty NextEntryProperty =
BindableProperty.Create(
nameof(NextEntry),
typeof(ExtendedEntry),
typeof(SingleDigitEntry),
null);
public ExtendedEntry NextEntry
{
get => (ExtendedEntry)GetValue(NextEntryProperty);
set => SetValue(NextEntryProperty, value);
}
private static readonly BindableProperty CornerRadiusProperty =
BindableProperty.Create(
nameof(CornerRadius),
typeof(float),
typeof(SingleDigitEntry),
null);
public float CornerRadius
{
get => (float)GetValue(CornerRadiusProperty);
set => SetValue(CornerRadiusProperty, value);
}
private Color _currentBorderColor;
public Color CurrentBorderColor
{
get => _currentBorderColor;
set
{
_currentBorderColor = value;
OnPropertyChanged(nameof(CurrentBorderColor));
}
}
private Color _currentBackgroundColor;
public Color CurrentBackgroundColor
{
get => _currentBackgroundColor;
set
{
_currentBackgroundColor = value;
OnPropertyChanged(nameof(CurrentBackgroundColor));
}
}
private void OnFocused(object sender, FocusEventArgs e)
{
CurrentBackgroundColor = BackgroundColorActive;
CurrentBorderColor = BorderColorActive;
}
private void OnUnfocused(object sender, FocusEventArgs e)
{
CurrentBackgroundColor = BackgroundColorInactive;
CurrentBorderColor = BorderColorInactive;
}
}
This is my xaml:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:renderers="clr-namespace:Test.Renderers;assembly=Test.Renderers" x:Class="Test.Controls.SingleDigitEntry" x:Name="this">
<Frame IsClippedToBounds="True" BackgroundColor="{Binding CurrentBorderColor, Source={x:Reference this}}" Padding="1, 1, 1, 1" CornerRadius="5" HasShadow="False" HorizontalOptions="FillAndExpand" ><Frame BackgroundColor="{Binding CurrentBackgroundColor, Source={x:Reference this}}" Padding="0" CornerRadius="5" HorizontalOptions="FillAndExpand" HasShadow="False">
<renderers:ExtendedEntry Style="{StaticResource OneDigitEntry}" CornerRadius="5" Focused="OnFocused" Unfocused="OnUnfocused" />
</Frame>
</Frame>
</ContentView>
And last but not least, the usage in my content page:
<controls:SingleDigitEntry BackgroundColorActive="YellowGreen" BorderColorActive="YellowGreen" BorderColorInactive="Red" BackgroundColorInactive="Red" />