Моя проблема заключается в том, что когда я использую ScaleTransform для элемента управления WebBrowser, он переполняет свой контейнер StackPanel, даже если у меня ClipToBounds = "True". Я пытаюсь добавить возможность увеличения в WebBrowser, но хотел бы, чтобы вертикальные и горизонтальные полосы прокрутки активировались в StackPanel, когда WebBrowser перерастает видимое пространство. Пока все работает нормально в моем коде, но я не могу понять, как предотвратить масштабирование WebBrowser от его родительского элемента. Спасибо за любую помощь!
Xaml
<Window
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"
mc:Ignorable="d" x:Class="WpfApplication1.MainWindow"
Title="Dialog" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen" Height="775" Width="1043" >
<Grid Height="623" Width="572">
<Border ClipToBounds="True" >
<Grid>
<DockPanel Background="#FFE8E8E8" Height="28" LastChildFill="False" VerticalAlignment="Top">
<Button Name="btnZoomOut" MouseUp="Image_MouseLeftButtonUp_1" Click="Image_MouseLeftButtonUp_1">Zoom Out</Button>
<Button Name="btnZoomIn" MouseUp="Image_MouseLeftButtonUp" Click="Image_MouseLeftButtonUp">Zoom In</Button>
</DockPanel>
<Grid ClipToBounds="True" Margin="0,28,0,0">
<ScrollViewer VerticalScrollBarVisibility="Auto" CanContentScroll="False">
<StackPanel Name="Container" Orientation="Vertical" Background="Silver" ClipToBounds="True">
<WebBrowser x:Name="webBrowser1" Margin="0,10,0,10" Source="http://msdn.com" />
</StackPanel>
</ScrollViewer>
</Grid>
</Grid>
</Border>
</Grid>
C #:
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Navigation;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
webBrowser1.Margin = new System.Windows.Thickness(0, 10, 0, 0);
webBrowser1.LoadCompleted += WebBrowser1_LoadCompleted;
//webBrowser1.NavigateToString(text);
ScaleWebBrowser(webBrowser1);
}
private void SetZoom(WebBrowser wb)
{
var wbv = (dynamic)wb.GetType().GetField("_axIWebBrowser2",
BindingFlags.Instance | BindingFlags.NonPublic)
.GetValue(wb);
int zoomFActor = (int)(scalefactor * 100);
// Between 10 and 1000
wbv.ExecWB(63, 2, zoomFActor, ref zoomFActor);
}
private void WebBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
//Try to Size WebBrowser to loaded document
mshtml.IHTMLDocument2 docs2 = (mshtml.IHTMLDocument2)wb.Document;
mshtml.IHTMLDocument3 docs3 = (mshtml.IHTMLDocument3)wb.Document;
mshtml.IHTMLElement2 body2 = (mshtml.IHTMLElement2)docs2.body;
mshtml.IHTMLElement2 root2 = (mshtml.IHTMLElement2)docs3.documentElement;
int width = Math.Max(body2.scrollWidth, root2.scrollWidth);
int height = Math.Max(root2.scrollHeight, body2.scrollHeight);
//Resize the control to the exact size to display the page.Also, make sure scroll bars are disabled
wb.Width = width;
wb.Height = height;
//Set an initial zoom
SetZoom(wb);
}
double scalefactor = 0.5;
private void ScaleWebBrowser(WebBrowser wb)
{
if (scalefactor >= 2)
scalefactor = 2;
if (scalefactor <= 0.05)
scalefactor = 0.05;
wb.LayoutTransform = new ScaleTransform(scalefactor, scalefactor);
}
private double DecWebBrowser()
{
return 0.0;
}
private void IncContent()
{
}
private void DecContent()
{
}
private void Image_MouseLeftButtonUp_1(object sender, RoutedEventArgs e)
{
scalefactor -= 0.05;
ScaleWebBrowser(webBrowser1);
SetZoom(webBrowser1);
}
private void Image_MouseLeftButtonUp(object sender, RoutedEventArgs e)
{
scalefactor += 0.05;
ScaleWebBrowser(webBrowser1);
SetZoom(webBrowser1);
}
}
}