Skip to content

Commit 137a4bb

Browse files
committed
WPF - Update CleanupElement when PresentationSource changes
- If the CleanupElement is a Window then move it to the new Window
1 parent 4dba519 commit 137a4bb

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Window x:Class="CefSharp.Wpf.Example.ChangeParentWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
mc:Ignorable="d"
7+
Title="ChangeParentWindow" Height="600" Width="800" WindowStartupLocation="CenterScreen" WindowState="Maximized">
8+
<Grid>
9+
<Grid.RowDefinitions>
10+
<RowDefinition Height="Auto" />
11+
<RowDefinition Height="*" />
12+
</Grid.RowDefinitions>
13+
<Grid.ColumnDefinitions>
14+
<ColumnDefinition />
15+
<ColumnDefinition />
16+
</Grid.ColumnDefinitions>
17+
<StackPanel Orientation="Horizontal" Grid.ColumnSpan="2">
18+
<Button Content="Open browser in new Window" Click="OnAddBrowser" />
19+
<Button Content="Re-Parent Browser and Close Window" Click="OnRemoveBrowser" />
20+
</StackPanel>
21+
<Border Grid.Row="1" Grid.Column="0" x:Name="StaticBrowser" />
22+
<Border Grid.Row="1" x:Name="BrowserSite" Grid.Column="1" />
23+
</Grid>
24+
</Window>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright © 2022 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using System.Windows;
6+
7+
namespace CefSharp.Wpf.Example
8+
{
9+
/// <summary>
10+
/// Interaction logic for ChangeParentWindow.xaml
11+
/// </summary>
12+
public partial class ChangeParentWindow : Window
13+
{
14+
private ChromiumWebBrowser browser;
15+
private Window newWindow;
16+
17+
public ChangeParentWindow()
18+
{
19+
InitializeComponent();
20+
21+
StaticBrowser.Child = new ChromiumWebBrowser("http://www.msn.net");
22+
}
23+
24+
private void OnAddBrowser(object sender, RoutedEventArgs e)
25+
{
26+
if (browser == null)
27+
{
28+
browser = new ChromiumWebBrowser("http://www.msn.net");
29+
}
30+
31+
if (newWindow == null)
32+
{
33+
newWindow = new Window
34+
{
35+
Owner = this,
36+
Content = browser,
37+
WindowStartupLocation = WindowStartupLocation.CenterOwner
38+
};
39+
40+
newWindow.Show();
41+
}
42+
}
43+
44+
private void OnRemoveBrowser(object sender, RoutedEventArgs e)
45+
{
46+
if (newWindow != null)
47+
{
48+
newWindow.Content = null;
49+
newWindow.Close();
50+
newWindow = null;
51+
}
52+
53+
BrowserSite.Child = browser;
54+
}
55+
}
56+
}
57+

CefSharp.Wpf/ChromiumWebBrowser.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,19 @@ private void PresentationSourceChangedHandler(object sender, SourceChangedEventA
16551655
window.StateChanged += OnWindowStateChanged;
16561656
window.LocationChanged += OnWindowLocationChanged;
16571657
sourceWindow = window;
1658+
1659+
if (CleanupElement == null)
1660+
{
1661+
CleanupElement = window;
1662+
}
1663+
else if(CleanupElement is Window parent)
1664+
{
1665+
//If the CleanupElement is a window then move it to the new Window
1666+
if(parent != window)
1667+
{
1668+
CleanupElement = window;
1669+
}
1670+
}
16581671
}
16591672

16601673
browserScreenLocation = GetBrowserScreenLocation();
@@ -1941,11 +1954,6 @@ private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArg
19411954
/// <param name="routedEventArgs">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
19421955
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
19431956
{
1944-
if (CleanupElement == null)
1945-
{
1946-
CleanupElement = Window.GetWindow(this);
1947-
}
1948-
19491957
// TODO: Consider making the delay here configurable.
19501958
tooltipTimer = new DispatcherTimer(
19511959
TimeSpan.FromSeconds(0.5),

0 commit comments

Comments
 (0)