WPF 将视觉画笔的视觉效果绑定到不同的窗口
我的设置窗口中需要一个矩形来显示主窗口的缩小版本。这是我现在拥有的无效代码。可以做我想做的事吗?
<Rectangle.Fill>
<VisualBrush Stretch="Uniform" Visual="{Binding ElementName=local:MainWindow}" />
</Rectangle.Fill>
I need a rectangle in my settings window to display a scaled down version of of the main window. This is the non-working code that I have right now. Is it possible to do what I want to do?
<Rectangle.Fill>
<VisualBrush Stretch="Uniform" Visual="{Binding ElementName=local:MainWindow}" />
</Rectangle.Fill>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,但不是在纯 XAML 中并且不使用 ElementName。相反,您需要将对主窗口的引用传递到设置窗口中。然后,您可以将 VisualBrush.Visual 绑定到该引用。
作为一个简化的示例,在创建设置窗口时,您可以将其 DataContext 设置为主窗口:
然后您可以通过
{Binding}
访问 MainWindow (因为 MainWindow 现在是 SettingsWindow 的 DataContext,和{Binding}
指的是 DataContext):实际上,您可能不希望将主窗口对象作为 DataContext 传递,因为这太生硬了,但希望这能给您带来启发。
Yes, but not in pure XAML and not using ElementName. Instead, you'll need to pass a reference to the main window into your settings window. You can then bind the VisualBrush.Visual to that reference.
As a simplified example, when creating your settings window, you could set its DataContext to the main window:
Then the SettingsWindow you could access the MainWindow as
{Binding}
(because the MainWindow is now the SettingsWindow's DataContext, and{Binding}
refers to the DataContext):In practice you probably won't want to pass the main window object as the DataContext because that's too blunt an instrument, but hopefully this gives you the idea.