WPF Binding a visual brush's visual to a different window

那年仲夏 提交于 2019-11-30 05:22:13

问题


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>

回答1:


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:

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

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>

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.



来源:https://stackoverflow.com/questions/2573136/wpf-binding-a-visual-brushs-visual-to-a-different-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!