Replies: 3 comments 3 replies
-
This boils down to resizing and moving a Window. For these API: |
Beta Was this translation helpful? Give feedback.
-
Are you saying that you only want to recentre the window if the content of the window causes the size to change and not change the position of the window if the user manually resizes the window? Or do you just want to give the user some time to change the size of the window before you recentre the window for them? If you want the latter then maybe using something like the |
Beta Was this translation helpful? Give feedback.
-
Try this: using Avalonia;
using Avalonia.Controls;
namespace AvaloniaResizingDemo;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// initial centre
WindowStartupLocation = WindowStartupLocation.CenterScreen;
SizeToContent = SizeToContent.WidthAndHeight;
// keep it centred when *we* (or layout) resize it
Resized += OnWindowResized;
}
private void OnWindowResized(object? sender, WindowResizedEventArgs e)
{
// Ignore live user-drags – they should keep control.
if (e.Reason == WindowResizeReason.User)
return;
CenterInWorkingArea();
}
private void CenterInWorkingArea()
{
// Which screen is the window on?
var screen = Screens.ScreenFromWindow(this) ?? Screens.Primary;
if (screen is null) return;
var wa = screen.WorkingArea; // PixelRect
var newLeft = wa.X + (wa.Width - (int)ClientSize.Width) / 2;
var newTop = wa.Y + (wa.Height - (int)ClientSize.Height) / 2;
Position = new PixelPoint(newLeft, newTop);
}
} <Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="AvaloniaResizingDemo.MainWindow"
Title="AvaloniaResizingDemo">
<StackPanel>
<TextBlock Text="{Binding #TextBox.Text}" />
<TextBox x:Name="TextBox" Text="Welcome to Avalonia!"
AcceptsReturn="True" AcceptsTab="True"/>
</StackPanel>
</Window> |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'd like to center a Window automatically. My Window has SizeToContent, so it can grow. This breaks UX, because initially it in the center of the screen. I would like it to automatically center itself again.
I have made it work partially intercepting
Window.LayoutUpdated
, but it tries to center the Window even when the user resizes the window manually, that produces a very glitchy behavior.How can I avoid re-centering when the user is resizing it manually? I haven't found any way to distinguish between manual and "programmatic" layout updates.
Beta Was this translation helpful? Give feedback.
All reactions