用于WPF的Silverlight儿童窗口
除了WPF,是否可以在Silverlight中创建类似ChildWindow的ChildWindow?我尝试将Silverlight ChildWindow调整为WPF,但遇到了转换问题,无法设置弹出窗口的父窗口。我正在尝试使一些东西能够同时工作,这样我就不必为弹出窗口向XAML添加代码。有什么想法吗?只需从Windows派生,然后从父窗口调用ShowDialog。这个类应该做您想做的事情:用于WPF的Silverlight儿童窗口,wpf,silverlight,childwindow,Wpf,Silverlight,Childwindow,除了WPF,是否可以在Silverlight中创建类似ChildWindow的ChildWindow?我尝试将Silverlight ChildWindow调整为WPF,但遇到了转换问题,无法设置弹出窗口的父窗口。我正在尝试使一些东西能够同时工作,这样我就不必为弹出窗口向XAML添加代码。有什么想法吗?只需从Windows派生,然后从父窗口调用ShowDialog。这个类应该做您想做的事情: public class SilverlightishPopup { private Recta
public class SilverlightishPopup
{
private Rectangle maskRectangle = new Rectangle { Fill = new SolidColorBrush(Colors.DarkGray), Opacity = 0.0 };
public FrameworkElement Parent
{
get;
set;
}
public FrameworkElement Content
{
get;
set;
}
public SilverlightishPopup()
{
Button button = new Button();
button.Width = 100;
button.Height = 200;
button.Content = "I am the popup!";
button.Click += delegate { Close(); };
Content = button;
}
public void Show()
{
Grid grid = GetRootGrid();
if (grid != null)
{
DoubleAnimation opacityAnimation = new DoubleAnimation(0.5, new Duration(TimeSpan.FromSeconds(0.5)));
Storyboard opacityBoard = new Storyboard();
opacityBoard.Children.Add(opacityAnimation);
Storyboard.SetTarget(opacityAnimation, maskRectangle);
Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("(Opacity)"));
opacityBoard.Completed += delegate
{
ScaleTransform scaleTransform = new ScaleTransform(0.0, 0.0, Content.Width / 2.0, Content.Height / 2.0);
Content.RenderTransform = scaleTransform;
grid.Children.Add(Content);
Storyboard scaleBoard = new Storyboard();
DoubleAnimation scaleXAnimation = new DoubleAnimation(1.0, TimeSpan.FromSeconds(0.5));
scaleBoard.Children.Add(scaleXAnimation);
Storyboard.SetTarget(scaleXAnimation, Content);
Storyboard.SetTargetProperty(scaleXAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)"));
DoubleAnimation scaleYAnimation = new DoubleAnimation(1.0, TimeSpan.FromSeconds(0.5));
scaleBoard.Children.Add(scaleYAnimation);
Storyboard.SetTarget(scaleYAnimation, Content);
Storyboard.SetTargetProperty(scaleYAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleY)"));
scaleBoard.Begin();
};
opacityBoard.Begin();
grid.Children.Add(maskRectangle);
}
}
public void Close()
{
Grid grid = GetRootGrid();
if (grid != null)
{
ScaleTransform scaleTransform = new ScaleTransform(1.0, 1.0, Content.Width / 2.0, Content.Height / 2.0);
Content.RenderTransform = scaleTransform;
Storyboard scaleBoard = new Storyboard();
DoubleAnimation scaleXAnimation = new DoubleAnimation(0.0, TimeSpan.FromSeconds(0.5));
scaleBoard.Children.Add(scaleXAnimation);
Storyboard.SetTarget(scaleXAnimation, Content);
Storyboard.SetTargetProperty(scaleXAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)"));
DoubleAnimation scaleYAnimation = new DoubleAnimation(0.0, TimeSpan.FromSeconds(0.5));
scaleBoard.Children.Add(scaleYAnimation);
Storyboard.SetTarget(scaleYAnimation, Content);
Storyboard.SetTargetProperty(scaleYAnimation, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleY)"));
scaleBoard.Completed += delegate
{
DoubleAnimation opacityAnimation = new DoubleAnimation(0.5, 0.0, new Duration(TimeSpan.FromSeconds(0.5)));
Storyboard opacityBoard = new Storyboard();
opacityBoard.Children.Add(opacityAnimation);
Storyboard.SetTarget(opacityAnimation, maskRectangle);
Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("(Opacity)"));
opacityBoard.Completed += delegate
{
grid.Children.Remove(maskRectangle);
grid.Children.Remove(Content);
};
opacityBoard.Begin();
};
scaleBoard.Begin();
}
}
private Grid GetRootGrid()
{
FrameworkElement root = Parent;
while (root is FrameworkElement && root.Parent != null)
{
FrameworkElement rootElement = root as FrameworkElement;
if (rootElement.Parent is FrameworkElement)
{
root = rootElement.Parent as FrameworkElement;
}
}
ContentControl contentControl = root as ContentControl;
return contentControl.Content as Grid;
}
}
只需将Parent属性设置为父窗口中的任何框架元素(它将找到该窗口以用掩码阻止它),并将内容设置为您希望弹出的内容(当然,当您希望显示它时,调用Show方法)。您必须自己设计弹出式包装器(即带有边框和一个调用close方法的close按钮的东西),但这并不困难,而且显然要删除构造函数中的占位符按钮(它只是在那里向您展示它的外观)
唯一的问题是,它只在有内容的窗口(即Silverlight中名为“LayoutRoot”的窗口)是网格(创建新WPF/Silverlight窗口/页面时的默认设置)上工作。我将它设置为适用于所有面板,但与StackPanel或DockPanel一起使用时,它看起来很奇怪(如预期的那样)。如果这对你不起作用,让我知道,我们会想办法的
如果使用它,您可能会使动画看起来更接近原始弹出窗口(可能使用一些缓和)。还有一种更好的方法可以找到根目录,我刚刚动态地提出了这个方法,但我认为它会起作用(尽管同样,只有内容设置为网格的Contentcontrol才能起作用)
如果您有任何问题,请与我联系,我希望这能解决您的问题。请查看。游戏概述完全符合您的要求。请参阅扩展WPF工具包中提供的ChildWindow控件
那很好,但我不想要新的窗户。我希望能够在当前窗口上显示面纱,并在当前窗口内显示新的子窗口。我完全熟悉windows的工作方式以及如何制作窗口和对话框,但这不是我的目标。我的目标正如我在上面的帖子中所说的,与silverlight的ChildWindow一样,只是在WPF中。奇怪的是,这正是我想要的,但它还没有被升级,所以我没有读它。我最终意识到WPF的窗口类是WPF中模式对话框所需要的。没错,它和Silverlight的儿童窗不完全一样。但它是模态的,只要您调用ShowDialog。如果我只是将构造函数更改为Window,并将Show更改为ShowDialog,那么我所有使用ChildWindow端口的Silverlight代码都很容易转换。我认为Silverlight的ChildWindow的Show不会阻止当前线程,而WPF的ShowDialog会阻止它,因此,您必须小心代码的逻辑不会中断。难道您不能在根目录之前动态注入一个网格父目录,并在完成后将其删除吗?他们已经更改了那里的ChildWindows和MessageBox类—“从版本2.0开始,MessageBox(和ChildWindow)从WindowControl派生而来,不再管理其父级的背景或基于父级大小的自身定位。现在应使用WindowContainer来包含这些控件。“顺便说一句,他们说旧版本将保持可用,但不确定它在最新版本的存储库中的位置。”