WPF将视觉笔刷的视觉内容绑定到其他窗口 [英] WPF Binding a visual brush's visual to a different window

查看:120
本文介绍了WPF将视觉笔刷的视觉内容绑定到其他窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在设置窗口中显示一个矩形,以显示主窗口的缩小版本.这是我现在无法使用的代码.可以做我想做的事吗?

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>

推荐答案

是的,但不是在纯XAML中并且未使用ElementName.相反,您需要将对主窗口的引用传递到设置窗口中.然后,您可以将VisualBrush.Visual绑定到该引用.

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.

作为一个简化的示例,在创建设置窗口时,可以将其DataContext设置为主窗口:

As a simplified example, when creating your settings window, you could set its DataContext to the main window:

// MainWindow.xaml.cs
SettingsWindow w = new SettingsWindow { DataContext = this };
w.Show();

然后,SettingsWindow可以以{Binding}身份访问MainWindow(因为MainWindow现在是SettingsWindow的DataContext,而{Binding}则是DataContext):

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):

<!-- SettingsWindow.xaml -->
<Rectangle.Fill>
  <VisualBrush Stretch="Uniform" Visual="{Binding}" />
</Rectangle.Fill>

在实践中,您可能不希望将主窗口对象作为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.

这篇关于WPF将视觉笔刷的视觉内容绑定到其他窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆