Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 鼠标位置上的WPF缩放画布中心_C#_Wpf_Xaml_Mvvm_Mvvm Light - Fatal编程技术网

C# 鼠标位置上的WPF缩放画布中心

C# 鼠标位置上的WPF缩放画布中心,c#,wpf,xaml,mvvm,mvvm-light,C#,Wpf,Xaml,Mvvm,Mvvm Light,参照点固定在左侧和顶部,用于缩放画布。我想放大和缩小鼠标指针。我如何使其成为MVVM模式?不在代码后面。使用鼠标滚轮,我可以放大画布。我已经使用了RenderTransferMorigin、CenterX和CenterY,但它不起作用。我想我走错了路。请帮助我。由于您没有提供当前的缩放代码,以下是鼠标位置缩放的一般示例: <utils:ScrollViewer x:Name="ImageViewer" VerticalScrollBarVisibility="Auto" Horizonta

参照点固定在左侧和顶部,用于缩放画布。我想放大和缩小鼠标指针。我如何使其成为MVVM模式?不在代码后面。使用鼠标滚轮,我可以放大画布。我已经使用了RenderTransferMorigin、CenterX和CenterY,但它不起作用。我想我走错了路。请帮助我。

由于您没有提供当前的缩放代码,以下是鼠标位置缩放的一般示例:

<utils:ScrollViewer x:Name="ImageViewer" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Row="2"                                                                   
                                       CurrentHorizontalOffset="{Binding ScrollHorizontalValue, Mode=TwoWay}"
                                       CurrentVerticalOffset="{Binding ScrollVerticalValue, Mode=TwoWay}"                                        
                                       >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="PreviewMouseWheel">
                            <cmd:EventToCommand Command="{Binding MouseWheelZoomCommand}" PassEventArgsToCommand="True"/>
                        </i:EventTrigger>
                        <i:EventTrigger EventName="ScrollChanged">
                            <cmd:EventToCommand Command="{Binding ScrollChangedCommand}" PassEventArgsToCommand="True"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <Grid Background="{StaticResource ThatchBackground}" RenderTransformOrigin="0.5,0.5">
                        <ItemsControl ItemsSource="{Binding CanvasItems}" ItemTemplate="{StaticResource templateOfROI}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <Canvas x:Name="BackPanel"
                                        Width="{Binding DataContext.ImageWidth, ElementName=MainGrid}" 
                                        Height="{Binding DataContext.ImageHeight, ElementName=MainGrid}"
                                        ClipToBounds="True">
                                        <Canvas.Background>
                                            <ImageBrush x:Name="BackImage"
                                                        ImageSource="{Binding DataContext.SelectedImage.Path, ElementName=MainGrid}"/>
                                        </Canvas.Background>

                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="MouseRightButtonDown">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseRightCommand, ElementName=MainGrid}"/>
                                            </i:EventTrigger>
                                            <i:EventTrigger EventName="MouseLeftButtonDown">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseMoveStartCommand, ElementName=MainGrid}" PassEventArgsToCommand="True"/>
                                            </i:EventTrigger>
                                            <i:EventTrigger EventName="MouseMove">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseMovingCommand, ElementName=MainGrid}" PassEventArgsToCommand="True"/>
                                            </i:EventTrigger>
                                            <i:EventTrigger EventName="MouseRightButtonUp">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseMoveEndCommand, ElementName=MainGrid}"/>
                                            </i:EventTrigger>
                                            <i:EventTrigger EventName="MouseLeave">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseLeaveCommand, ElementName=MainGrid}"/>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>

                                        <Canvas.LayoutTransform>
                                            <TransformGroup>
                                                <ScaleTransform ScaleX="{Binding ScaleX}"
                                                                ScaleY="{Binding ScaleY}">
                                                </ScaleTransform>
                                        </TransformGroup>
                                    </Canvas.LayoutTransform>
                                </Canvas>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>
                </Grid>
            </utils:ScrollViewer>

由于您没有提供当前的缩放代码,以下是鼠标位置缩放的一般示例:

<utils:ScrollViewer x:Name="ImageViewer" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Grid.Row="2"                                                                   
                                       CurrentHorizontalOffset="{Binding ScrollHorizontalValue, Mode=TwoWay}"
                                       CurrentVerticalOffset="{Binding ScrollVerticalValue, Mode=TwoWay}"                                        
                                       >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="PreviewMouseWheel">
                            <cmd:EventToCommand Command="{Binding MouseWheelZoomCommand}" PassEventArgsToCommand="True"/>
                        </i:EventTrigger>
                        <i:EventTrigger EventName="ScrollChanged">
                            <cmd:EventToCommand Command="{Binding ScrollChangedCommand}" PassEventArgsToCommand="True"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                    <Grid Background="{StaticResource ThatchBackground}" RenderTransformOrigin="0.5,0.5">
                        <ItemsControl ItemsSource="{Binding CanvasItems}" ItemTemplate="{StaticResource templateOfROI}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <Canvas x:Name="BackPanel"
                                        Width="{Binding DataContext.ImageWidth, ElementName=MainGrid}" 
                                        Height="{Binding DataContext.ImageHeight, ElementName=MainGrid}"
                                        ClipToBounds="True">
                                        <Canvas.Background>
                                            <ImageBrush x:Name="BackImage"
                                                        ImageSource="{Binding DataContext.SelectedImage.Path, ElementName=MainGrid}"/>
                                        </Canvas.Background>

                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="MouseRightButtonDown">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseRightCommand, ElementName=MainGrid}"/>
                                            </i:EventTrigger>
                                            <i:EventTrigger EventName="MouseLeftButtonDown">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseMoveStartCommand, ElementName=MainGrid}" PassEventArgsToCommand="True"/>
                                            </i:EventTrigger>
                                            <i:EventTrigger EventName="MouseMove">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseMovingCommand, ElementName=MainGrid}" PassEventArgsToCommand="True"/>
                                            </i:EventTrigger>
                                            <i:EventTrigger EventName="MouseRightButtonUp">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseMoveEndCommand, ElementName=MainGrid}"/>
                                            </i:EventTrigger>
                                            <i:EventTrigger EventName="MouseLeave">
                                                <cmd:EventToCommand Command="{Binding DataContext.MouseLeaveCommand, ElementName=MainGrid}"/>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>

                                        <Canvas.LayoutTransform>
                                            <TransformGroup>
                                                <ScaleTransform ScaleX="{Binding ScaleX}"
                                                                ScaleY="{Binding ScaleY}">
                                                </ScaleTransform>
                                        </TransformGroup>
                                    </Canvas.LayoutTransform>
                                </Canvas>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                    </ItemsControl>
                </Grid>
            </utils:ScrollViewer>

我使用@Grek40的代码,并将其转换为任何试图坚持MVVM模式的人的行为。如果你投票支持这个,请记住也投票支持他的答案,因为我的答案是基于他的工作。它需要nuget包 或者行为基类的其他框架。您可以将其应用于任何UIElement。它将自动为您添加MatrixTransform,因此您不必在XAML中这样做,它将覆盖任何现有的转换

private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
{
    var matTrans = grid2.RenderTransform as MatrixTransform;
    var pos1 = e.GetPosition(grid1);

    var scale = e.Delta > 0 ? 1.1 : 1 / 1.1;

    var mat = matTrans.Matrix;
    mat.ScaleAt(scale, scale, pos1.X, pos1.Y);
    matTrans.Matrix = mat;
    e.Handled = true;
}

我使用@Grek40的代码,并将其转换为任何试图坚持MVVM模式的人的行为。如果你投票支持这个,请记住也投票支持他的答案,因为我的答案是基于他的工作。它需要nuget包 或者行为基类的其他框架。您可以将其应用于任何UIElement。它将自动为您添加MatrixTransform,因此您不必在XAML中这样做,它将覆盖任何现有的转换

private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
{
    var matTrans = grid2.RenderTransform as MatrixTransform;
    var pos1 = e.GetPosition(grid1);

    var scale = e.Delta > 0 ? 1.1 : 1 / 1.1;

    var mat = matTrans.Matrix;
    mat.ScaleAt(scale, scale, pos1.X, pos1.Y);
    matTrans.Matrix = mat;
    e.Handled = true;
}

处理缩放的代码在哪里?即使它没有按预期工作,您仍然应该将其作为当前方法。处理缩放的代码在哪里?即使它没有按预期工作,您仍然应该将其作为您当前的方法。我希望您不介意,但我根据您的代码添加了一个答案,它的形式为尝试坚持MVVM模式的人提供了一种行为。@BradleyUffner我一点也不介意。各种好的解决方案都有帮助,所以总体而言,我对你提到我的贡献的方式非常满意;这是一个非常简单的解决方案。我的问题是,是否有办法改变缩放内容的过滤方式,即使用最近邻?我将其用于具有平铺图像背景的画布,尽管尝试在图像上使用RenderOptions.BitmapScalingMode,但它没有效果,可能是因为渲染变换是在之后应用的。Hi@Logix问得好,但我不知道答案。也许可以检查与BitmapScalingMode相关的其他答案是否有帮助。否则,您可能应该打开一个新问题我希望您不介意,但我根据您的代码添加了一个答案,形式是试图坚持MVVM模式的人的行为。@BradleyUffner我一点也不介意。各种好的解决方案都有帮助,所以总体而言,我对你提到我的贡献的方式非常满意;这是一个非常简单的解决方案。我的问题是,是否有办法改变缩放内容的过滤方式,即使用最近邻?我将其用于具有平铺图像背景的画布,尽管尝试在图像上使用RenderOptions.BitmapScalingMode,但它没有效果,可能是因为渲染变换是在之后应用的。Hi@Logix问得好,但我不知道答案。也许可以检查与BitmapScalingMode相关的其他答案是否有帮助。否则,您可能应该打开一个新问题。我已经添加了仅在通过ModifierKey属性按住修改器键(如LeftCtrl)时滚动的功能,以及通过TransformMode属性控制转换是作为RenderTranform还是作为LayoutTransform完成的功能。它默认为RenderTransform,但如果将具有该行为的元素放置在scrollviewer中,并将其设置为LayoutTransform,则放大时可以获得滚动条。我添加了仅在通过ModifierKey属性按住修改键(如LeftCtrl)时滚动的功能,以及通过TransformMode属性控制转换是作为RenderTranform还是作为LayoutTransform完成的功能。它默认为RenderTransform,但如果将具有该行为的元素放置在scrollviewer中,并将其设置为LayoutTransform,则放大时可以获得滚动条。
<Grid>
    <interactivity:Interaction.Behaviors>
        <behaviors:ZoomOnMouseWheel />
    </interactivity:Interaction.Behaviors>
    <!--Your grid content here-->
</Grid>
xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"