首页 > 解决方案 > 如何将 BusyIndi​​cator 居中到 MainWindow 的中心?

问题描述

我创建了示例 Wpf 应用程序并安装了扩展 WPF 工具包(NuGet 包)。这是我用于显示 BusyIndi​​cator 的 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 屏幕中间显示 BusyIndi​​cator。如下图所示,它没有像应有的那样居中。请注意,我使用SizeToContent="WidthAndHeight"

在此处输入图像描述

标签: wpfwpftoolkit

解决方案


有一个单独的窗口来显示忙碌指示器,并会导致不需要的行为。如果原始窗口最大化、移动等会发生什么?

考虑将忙碌指示器添加到主屏幕。通常我会创建一个覆盖区域,用于显示消息对话框、进度条等。

<Window>
<Grid>
<Application stuff ....>
</Application stuff>
  <ContentControl regions:RegionManager.RegionName="OverlayRegion"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch" />
    </Grid>
</Window>

我在这里使用 Prism,但您可以将 ContentControl 替换为任何内容,例如 BusyIndi​​cator 并操纵控件的可见性。


推荐阅读