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

hen*_*ton 6 vb.net wpf

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

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

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

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

谢谢!

Mic*_*ter 8

您可以从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 enables 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">

                <!-- This ScaleTransform implements the zooming and is bound the Value of the ZoomSlider -->
                <StackPanel.LayoutTransform>
                    <ScaleTransform ScaleX="{Binding ElementName=ZoomSlider, Path=Value}" ScaleY="{Binding ElementName=ZoomSlider, Path=Value}" />
                </StackPanel.LayoutTransform>

                <!-- Here is your custom content -->
                <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>
Run Code Online (Sandbox Code Playgroud)


d.m*_*ada 7

这可能是一个很好的候选人Viewbox.

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

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

<Window>
   <Viewbox>
       <!-- content here -->
   </Viewbox>
</Window>
Run Code Online (Sandbox Code Playgroud)

然后绑定到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>
Run Code Online (Sandbox Code Playgroud)

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;   
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)