也许标题措辞不正确。
只要在 ChildWindow 打开时我不尝试使用它,我就有一个“全局”忙碌指示器,效果很好。
我使用 App.xaml.cs 中的静态方法访问“全局”忙碌指示器:
BusyIndicator b = (BusyIndicator)App.Current.RootVisual;
if (b != null)
{
b.BusyContent = busyText;
b.IsBusy = true;
}
但是,如果 ChildWindow 打开,BusyIndicator 总是在它后面。
我以为我可以设置b.Content = VisualTreeHelper.GetOpenPopups().First()
,但这也不起作用。
有没有人有关于在打开的 ChildWindows 之上使用 BusyIndicator 的任何提示?
提前致谢。
更新(解决方案)
Dave S 让我走上了正轨。它比我希望的要复杂,但这是我的解决方案。
首先,我必须为 ChildWindow 制作一个完整的样式,复制所有的 Template 样式(由于后期大小的原因而遗漏了一些):
<Style x:Key="MyChildWindowStyle" TargetType="gs:MyChildWindow">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="gs:MyChildWindow">
<toolkit:BusyIndicator IsBusy="{TemplateBinding IsBusy}" BusyContent="{TemplateBinding BusyContent}" BusyContentTemplate="{StaticResource MyBusyIndicatorDataTemplate}">
<Grid>
<ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Grid>
</toolkit:BusyIndicator>
</Style>
然后,我创建了我的基类;注意构造函数设置样式。(如果我试图使其抽象,就会发生错误。)
public class MyChildWindow : ChildWindow
{
public static readonly DependencyProperty IsBusyProperty = DependencyProperty.Register("IsBusy", typeof(bool), typeof(MyChildWindow), null);
public static readonly DependencyProperty BusyContentProperty = DependencyProperty.Register("BusyContent", typeof(object), typeof(MyChildWindow), null);
public bool IsBusy
{
get { return (bool)GetValue(IsBusyProperty); }
set { SetValue(IsBusyProperty, value); }
}
public object BusyContent
{
get { return GetValue(BusyContentProperty); }
set { SetValue(BusyContentProperty, value); }
}
public MyChildWindow()
{
this.Style = Application.Current.Resources["MyChildWindowStyle"] as Style;
}
}
确保添加一个新的 ChildWindow,并将 <controls:ChildWindow 更改为 <gs:MyChildWindow(与代码隐藏相同)。
最后,更新静态 SetBusyIndicator 方法:
public static void SetBusyIndicator(string busyText, Uri busyImage)
{
var op = VisualTreeHelper.GetOpenPopups().Where(o => o.Child is MyChildWindow);
if (op.Any())
{
var bidc = new MyBusyIndicatorDataContext(busyText, busyImage);
foreach (System.Windows.Controls.Primitives.Popup p in op)
{
var c = p.Child as MyChildWindow;
c.BusyContent = bidc;
c.IsBusy = true;
}
}
else
{
BusyIndicator b = Current.RootVisual as BusyIndicator;
if (b != null)
{
b.BusyContent = new MyBusyIndicatorDataContext(busyText, busyImage);
b.IsBusy = true;
}
}
}
我不确定这是最有效的,但它似乎工作得很好。