创建允许缩放和平移的WPF窗口

时间:2014-03-18 19:40:51

标签: wpf vb.net

我想创建一个可以容纳多个控件的Window。但是,我希望用户能够平移并放大和缩小以查看这些控件的更大版本。

我甚至不知道从哪里开始寻找。

我将开始使用ScaleTransform来响应鼠标上滚动按钮的使用,但我不确定这是不是最好的主意。

只需要朝着正确的方向努力。

谢谢!

3 个答案:

答案 0 :(得分:6)

这可能是Viewbox的合适人选。

见这里:http://msdn.microsoft.com/en-us/library/system.windows.controls.viewbox(v=vs.110).aspx

基本上,您可以将窗口的全部内容包装成Viewbox,如下所示:

<Window>
   <Viewbox>
       <!-- content here -->
   </Viewbox>
</Window>

然后绑定到Viewbox控件的宽度和高度以模拟缩放。对于快速测试,您可以通过代码隐藏来监听滚轮事件,命名Viewbox控件,并在更改其中的值时直接访问Viewbox。

编辑:这是我刚刚找到的让您入门的方案。他们正在使用图像,但它与我上面描述的完全相同。

http://www.c-sharpcorner.com/uploadfile/yougerthen/working-with-wpf-viewbox-control/

Edit2:使用鼠标滚动的快速工作示例

的Xaml:

<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;   
            }
        }
    }
}

答案 1 :(得分:6)

您可以从ScrollViewer和ScaleTransform中获取功能。这是一个例子:

<Window x:Class="CSharpWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <!-- This ScrollViewer enabled the "panning" -->
        <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">

            <!-- This StackPanel is the container for the zoomable/pannable content. Any container control (StackPanel, DockPanel, Grid, etc) may be used here. -->
            <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top">
                <StackPanel.LayoutTransform>
                    <!-- This ScaleTransform implements the zooming and is bound the Value of the ZoomSlider -->
                    <ScaleTransform ScaleX="{Binding ElementName=ZoomSlider, Path=Value}" ScaleY="{Binding ElementName=ZoomSlider, Path=Value}" />
                </StackPanel.LayoutTransform>
                <Button>Foo</Button>
                <Button>Bar</Button>
            </StackPanel>

        </ScrollViewer>

        <!-- This Slider controls the zoom level -->
        <Slider x:Name="ZoomSlider" Orientation="Horizontal" Grid.Row="1" Minimum="0.0" Maximum="8.0" LargeChange="0.25" SmallChange="0.01"  Value="1.0" />

    </Grid>


</Window>

答案 2 :(得分:0)

简单的解决方案:

    private void Window_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (Keyboard.Modifiers != ModifierKeys.Control)
            return;

        if (e.Delta < 0 && _scale > 0.7)
        {
            _scale -= 0.1;
            MainGrid.LayoutTransform = new ScaleTransform(_scale, _scale);
        }               
        else if (e.Delta > 0 && _scale < 1.5)
        {
            _scale += 0.1;
            MainGrid.LayoutTransform = new ScaleTransform(_scale, _scale);
        }
            
    }