It is recommended that you directly set the background color of the html to #F222. It is currently difficult to set the background color of the WebBrowser(wpf) to a custom color.
Even using Winform's WebBrowser can only set the html document page to black instead of #F222.
An example of using Winform's Webbrowser in wpf is as follows:
1.Right-click the References
under the project and select Add Reference...
2.Search for adding System.Windows.Forms
and WindowsFormsIntegration
references and click OK.
3.Add namespace in Window.
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
complete code:
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="1200">
<StackPanel x:Name="grid" Background="#f222" Orientation="Horizontal" >
<WindowsFormsHost x:Name="wfhSample" Width="400" Height="400" Canvas.Right="10">
<WindowsFormsHost.Child>
<wf:WebBrowser x:Name="wb" DocumentCompleted="wb_DocumentCompleted"/>
</WindowsFormsHost.Child>
</WindowsFormsHost>
</StackPanel>
</Window>
MainWindow.xaml.cs:
using System.Windows;
using System.Windows.Forms;
namespace WpfApp2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
wb.Navigate("http://google.com");
}
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument document = (HtmlDocument)wb.Document;
document.Body.Style = "background-color:black";
}
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.