0

我有一个 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" />

现在,在代码中,我有带有以下代码的 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 或其他一些程序?

谢谢

对不起,我的英语不好...

4

1 回答 1

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 上的鼠标位置。

于 2013-04-12T05:45:26.267 回答