wpf - 如何将 BusyIndicator 居中到 MainWindow 的中心?
问题描述
我创建了示例 Wpf 应用程序并安装了扩展 WPF 工具包(NuGet 包)。这是我用于显示 BusyIndicator 的 xaml 代码。
<Window x:Class="WpfApp3.Progress"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
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"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
WindowStyle="None"
BorderThickness="0"
Title=""
VerticalAlignment="Center"
HorizontalAlignment="Center"
SizeToContent="WidthAndHeight"
d:DesignWidth="300"
d:DesignHeight="300">
<xctk:BusyIndicator IsBusy="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"></xctk:BusyIndicator>
</Window>
使用以下代码触发显示进度窗口:
private void Button_Click(object sender, RoutedEventArgs e)
{
Progress w = new Progress
{
Owner = Application.Current.MainWindow,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
w.Show();
}
我的问题很简单。如何在 MainWindow 屏幕中间显示 BusyIndicator。如下图所示,它没有像应有的那样居中。请注意,我使用SizeToContent="WidthAndHeight"
解决方案
有一个单独的窗口来显示忙碌指示器,并会导致不需要的行为。如果原始窗口最大化、移动等会发生什么?
考虑将忙碌指示器添加到主屏幕。通常我会创建一个覆盖区域,用于显示消息对话框、进度条等。
<Window>
<Grid>
<Application stuff ....>
</Application stuff>
<ContentControl regions:RegionManager.RegionName="OverlayRegion"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
</Grid>
</Window>
我在这里使用 Prism,但您可以将 ContentControl 替换为任何内容,例如 BusyIndicator 并操纵控件的可见性。
推荐阅读
- python - For 循环在字符串列的函数中不起作用
- flutter - 如何在颤动中修复“表格包含不规则的行长”?
- numpy - 在 Ubuntu 平台上使用带有 anaconda 环境的 numpy 库的问题
- aggregate - 一切都必须是聚合吗?多对多链接
- dictionary - 为什么使用 F# 的惯用字典集合(Map
) 需要类型 K 来实现可比较而 C# 的 Dictionary 不是吗? - java - 正则表达式在字符串模式之间查找文本
- c++ - 了解使用 SSE2 / SSSE3 指令集有效实现中值滤波器
- rust - Rust 类型中的下划线是什么?
- javascript - 与 cellFocused 相对的 Ag-grid 方法
- numpy - 如何根据行长(逐个文件)有选择地使用 sed 或 awk