WPF DataGrid缩放到鼠标位置

时间:2013-04-04 12:32:55

标签: wpf datagrid mouse zoom

我有一个DataGrid,LayoutTransform被绑定到Slider:

<DataGrid.LayoutTransform>
            <ScaleTransform 
                ScaleX="{Binding ElementName=MySlider, Path=Value}"
                ScaleY="{Binding ElementName=MySlider, Path=Value}" />
        </DataGrid.LayoutTransform>
    </DataGrid>

    <Slider x:Name="MySlider"
            Minimum="0.3"
            Maximum="2.0"
            SmallChange="0.1"
            LargeChange="0.1"
            Value="1.0" 
            IsSnapToTickEnabled="True"
            TickFrequency="0.1"
            TickPlacement="TopLeft"
            VerticalAlignment="Bottom"
            HorizontalAlignment="Right"
            Width="200"
            Margin="0,0,61,0"  />

    <TextBlock Name="Lstate"
               Text="{Binding ElementName=MySlider, Path=Value, StringFormat={}{0:P0}}"
               VerticalAlignment="Bottom"
               HorizontalAlignment="Right"
               Width="50" Height="20"
               Margin="0,0,0,1" />

现在,在Code中我有PreviewMouseWheel事件,其中包含以下代码:

bool handle = (Keyboard.Modifiers & ModifierKeys.Control) > 0;
        if (!handle)
            return;

        double value;

        if (e.Delta > 0)
            value = 0.1;
        else
            value = -0.1;

        MySlider.Value += value;

我的问题是:如何滚动到AutoCad或其他程序的实际鼠标位置?

由于

抱歉我的英语不好......

1 个答案:

答案 0 :(得分:0)

我现在有一个非常好的解决方案:

VirtualizingStackPanel.IsVirtualizing="True" 
VirtualizingStackPanel.VirtualizationMode="Standard"
EnableColumnVirtualization="False"
EnableRowVirtualization="True"
ScrollViewer.CanContentScroll="True"

    private void Data_OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        // Scroll to Zoom
        if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
        {
            // Prevent scroll
            e.Handled = true;

            var scrollview = FindVisualChild<ScrollViewer>(Data);
            if (scrollview != null)
            {  
                // The "+20" are there to account for the scrollbars... i think. Not perfectly accurate.
                var relativeMiddle = new Point((Data.ActualWidth + 20) / 2 + (Mouse.GetPosition(Data).X - Data.ActualWidth / 2), (Data.ActualHeight + 20) / 2 + (Mouse.GetPosition(Data).Y - Data.ActualHeight / 2));
                var oldLocation = Data.LayoutTransform.Transform(Data.PointFromScreen(relativeMiddle));

                // Zoom
                MySlider.Value += (e.Delta > 0) ? MySlider.TickFrequency : -MySlider.TickFrequency;

                // Scroll
                var newLocation = Data.LayoutTransform.Transform(Data.PointFromScreen(relativeMiddle));

                // Calculate offset
                var shift = newLocation - oldLocation;

                if (scrollview.CanContentScroll)
                {
                    // Scroll to the offset (Item)
                    scrollview.ScrollToVerticalOffset(scrollview.VerticalOffset + shift.Y / scrollview.ViewportHeight);
                }
                else
                {
                    // Device independent Pixels
                    scrollview.ScrollToVerticalOffset(scrollview.VerticalOffset + shift.Y);
                }

                // Device independent Pixels
                scrollview.ScrollToHorizontalOffset(scrollview.HorizontalOffset + shift.X);
            }
        }
    }

在使用和不使用虚拟化的情况下,它可以缩放到Datagrid上的鼠标位置。