我對我應該如何解決這個問題有某種有意義的誤解。我如何讓我的WPF scrollviewer與縮放工作?
我有一個ScrollViewer內的畫布。我希望能夠放大和縮小該Canvas並讓ScrollViewer適當調整。
下面的代碼:
XAML:
<Window x:Class="scrollerProblem.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<ScrollViewer Name="scroller" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Canvas Background="AntiqueWhite" Name="content" MouseLeftButtonDown="content_MouseLeftButtonDown" MouseRightButtonDown="content_MouseRightButtonDown">
<Rectangle Width="100" Height="100" Canvas.Top="50" Canvas.Left="50" Fill="PaleGoldenrod"></Rectangle>
</Canvas>
</ScrollViewer>
</Window>
而隱藏代碼:
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace scrollerProblem
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
float Zoom = 1;
public Window1()
{
InitializeComponent();
content.Width = 700;
content.Height = 700;
}
private void content_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
content.Width *= 2; // BLAH
content.Height *= 2; // BLAH
Zoom *= 2;
TransformGroup gridTransforms = new TransformGroup();
gridTransforms.Children.Add(new ScaleTransform(Zoom, Zoom));
gridTransforms.Children.Add(new TranslateTransform(0, 0));
content.RenderTransform = gridTransforms;
}
private void content_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
content.Width /= 2; // BLAH
content.Height /= 2; // BLAH
Zoom /= 2;
TransformGroup gridTransforms = new TransformGroup();
gridTransforms.Children.Add(new ScaleTransform(Zoom, Zoom));
gridTransforms.Children.Add(new TranslateTransform(0, 0));
content.RenderTransform = gridTransforms;
}
}
}
如果我離開了標有 「嗒嗒」 的臺詞滾動條根本不調整所有......這並不令人感到意外,因爲沒有實際改變內容畫布的大小。但是,如果我添加BLAH行,畫布會縮小/增大,但同時也會縮放,這意味着與其內容相比,它看起來不正確。
我猜我正在採取一種不正確的方法,但我不清楚如何解決它。這是正確的路,只是一個小問題,還是我在這裏完全錯誤的機智?
不會改變高度和寬度是否會影響ScrollViewer的實際像素大小(屏幕上)?這不是我想要做的。我希望ScrollViewer保持相同的可見尺寸......但是當內容太大時(當人放大到足夠遠時)顯示滾動條。 – Beska 2010-07-26 21:04:49