AutoCatAvalonia/AutoCat/Views/PlateView.axaml.cs

71 lines
1.9 KiB
C#

using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Markup.Xaml;
namespace AutoCat.Views;
public partial class PlateView : UserControl
{
#region Dependency properties
public static readonly StyledProperty<string> NumberProperty =
AvaloniaProperty.Register<PlateView, string>(nameof(Number));
#endregion
#region Properties
public string Number
{
get => (string)GetValue(NumberProperty);
set => SetValue(NumberProperty, value);
}
#endregion
private const double AspectRatio = 112.0 / 520.0;
static PlateView()
{
NumberProperty.Changed.AddClassHandler<PlateView>((v, e) => v.NumberChanged(e));
}
public PlateView()
{
InitializeComponent();
}
private void NumberChanged(AvaloniaPropertyChangedEventArgs e)
{
if (e.NewValue is not string newNumber)
return;
var number = new PlateNumber(newNumber);
NumberBox.Text = number.MainPart();
RegionBox.Text = number.Region();
}
protected override Size MeasureOverride(Size availableSize)
{
var newAspect = availableSize.Height / availableSize.Width;
return newAspect >= AspectRatio
? new Size(availableSize.Width, availableSize.Width * AspectRatio)
: new Size(availableSize.Height / AspectRatio, availableSize.Height);
}
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
NumberBox.Margin = new Thickness(0, -e.NewSize.Height * 0.35, 0, 0);
RegionBox.FontSize = e.NewSize.Height * 0.65;
RegionBox.Margin = new Thickness(0, -RegionBox.FontSize * 0.3, 0, 0);
FlagGrid.Width = FlagGrid.Height * 1.5;
if (CountryRow.Height > 0)
{
CountryName.FontSize = CountryRow.Height;
}
}
}