创建一个允许缩放和平移的 WPF 窗口 [英] Creating a WPF Window that allows zooming and panning
问题描述
我想创建一个包含多个控件的窗口.但是,我希望用户能够平移和放大和缩小以查看这些控件的较大版本.
I want to create a Window that will hold several controls. However, I would like the user to be able to pan around and zoom in and out to see larger versions of those controls.
我什至不知道从哪里开始寻找.
I don't even know where to begin looking.
我打算从响应鼠标滚动按钮使用的 ScaleTransform 开始,但我不确定这是否是最好的主意.
I was going to start at ScaleTransform that responds to the use of the scroll button on the mouse but I am not sure if that is the best idea.
只需要朝着正确的方向推动.
Just need a push in the right direction.
谢谢!
推荐答案
这可能是 Viewbox
的一个很好的候选者.
This might be a good candidate for a Viewbox
.
参见此处:http://msdn.microsoft.com/en-us/library/system.windows.controls.viewbox(v=vs.110).aspx
基本上,您可以像这样将 Window 的全部内容包装到 Viewbox
中:
Basically, you can Wrap the entire contents of the Window into a Viewbox
like so:
<Window>
<Viewbox>
<!-- content here -->
</Viewbox>
</Window>
然后绑定到Viewbox
控件的宽度和高度来模拟缩放.为了快速测试,您可以通过代码隐藏监听滚轮事件,命名 Viewbox 控件,并在更改值时直接访问 Viewbox.
and then bind to the Viewbox
control's width and height to simulate the zooming. For a quick test, you could just listen to scroll wheel events via code-behind, name the Viewbox control, and access the Viewbox directly at change the values there.
编辑:这是我刚刚发现的一个场景,可以帮助您入门.他们使用的是图像,但这与我上面描述的概念完全相同.
Edit: here's a scenario I just found to get you started. They are using an image, but it's the exact same concept that I described above.
http://www.c-sharpcorner.com/uploadfile/yougerthen/working-with-wpf-viewbox-control/
Edit2:使用鼠标滚动的快速工作示例
Quick working example using mouse-scroll
XML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
MouseWheel="MainWindow_OnMouseWheel">
<Grid>
<Viewbox x:Name="ZoomViewbox" Stretch="Fill">
<StackPanel>
<Label Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" />
</StackPanel>
</Viewbox>
</Grid>
</Window>
C#:
using System.Windows;
using System.Windows.Input;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ZoomViewbox.Width = 100;
ZoomViewbox.Height = 100;
}
private void MainWindow_OnMouseWheel(object sender, MouseWheelEventArgs e)
{
UpdateViewBox((e.Delta > 0) ? 5 : -5);
}
private void UpdateViewBox(int newValue)
{
if ((ZoomViewbox.Width >= 0) && ZoomViewbox.Height >= 0)
{
ZoomViewbox.Width += newValue;
ZoomViewbox.Height += newValue;
}
}
}
}
这篇关于创建一个允许缩放和平移的 WPF 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!