【发布时间】:2012-03-07 00:58:12
【问题描述】:
可能标题用错了。
我有一个很好用的“全局”忙碌指示器,只要我在 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,并将 最后,更新静态 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;
}
}
}
【问题讨论】:
标签: silverlight silverlight-toolkit childwindow busyindicator