TextDialogView.axaml.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Layout;
  4. using Avalonia.Markup.Xaml;
  5. using InABox.Avalonia.Converters;
  6. namespace InABox.Avalonia.Dialogs;
  7. internal class TextDialogVerticalAlignmentConverter : AbstractConverter<bool, VerticalAlignment>
  8. {
  9. protected override VerticalAlignment Convert(bool value, object? parameter = null)
  10. {
  11. return value ? VerticalAlignment.Top : VerticalAlignment.Center;
  12. }
  13. }
  14. public partial class TextDialogView : UserControl
  15. {
  16. private TextDialogViewModel Model => (DataContext as TextDialogViewModel)!;
  17. public TextDialogView()
  18. {
  19. InitializeComponent();
  20. TextBox.AttachedToVisualTree += TextBox_AttachedToVisualTree;
  21. }
  22. private void TextBox_AttachedToVisualTree(object? sender, VisualTreeAttachmentEventArgs e)
  23. {
  24. TextBox.Focus();
  25. }
  26. protected override Size MeasureOverride(Size availableSize)
  27. {
  28. var baseSize = base.MeasureOverride(availableSize);
  29. if (Model.Multiline)
  30. {
  31. return new(
  32. Double.IsPositiveInfinity(availableSize.Width) ? baseSize.Width : availableSize.Width,
  33. Double.IsPositiveInfinity(availableSize.Height) ? baseSize.Height : availableSize.Height);
  34. }
  35. return baseSize.WithWidth(
  36. Double.IsPositiveInfinity(availableSize.Width) ? baseSize.Width : availableSize.Width);
  37. }
  38. }