WPF snap a visual visual brush to another window

I need a rectangle in the settings window to display a smaller version of the main window. This is the broken code that I have right now. Can I do what I want to do?

<Rectangle.Fill> <VisualBrush Stretch="Uniform" Visual="{Binding ElementName=local:MainWindow}" /> </Rectangle.Fill> 
+5
source share
1 answer

Yes, but not in pure XAML and not using ElementName. Instead, you will need to transfer the link to the main window to the settings window. Then you can bind VisualBrush.Visual to this link.

As a simplified example, when creating a settings window, you can set its DataContext in the main window:

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

Then, in the SettingsWindow window, you can access the MainWindow as {Binding} (since MainWindow is now a DataContext SettingsWindow, and {Binding} refers to a 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 a DataContext, because these are too dumb tools, but hopefully this gives you an idea.

+7
source

Source: https://habr.com/ru/post/1305950/


All Articles