【问题标题】:WPF ScrollViewer HorizontalScrollBar dont work properlyWPF ScrollViewer Horizo​​ntalScrollBar 无法正常工作
【发布时间】:2013-10-21 04:47:58
【问题描述】:

我在 ScrollViewer 中有一个图像。当我将图像宽度设置得更大时,出现了 Horizo​​ntalScrollBar。然后我将图像宽度设置为小于 ScrollViewer With 但是这个 ScrollBar 仍然出现,像这样:

           
我该如何解决这个问题?谢谢!

<Grid>
    <ScrollViewer
        Name="sv"
        HorizontalScrollBarVisibility="Auto"
        VerticalScrollBarVisibility="Auto"
        PreviewMouseWheel="sv_PreviewMouseWheel">
        <Image Name="img"/>
    </ScrollViewer>
</Grid>

代码:

    void sv_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        if ((System.Windows.Forms.Control.ModifierKeys & System.Windows.Forms.Keys.Control) != System.Windows.Forms.Keys.Control) base.OnMouseWheel(e);
        else
        {
            if (e.Delta > 0)
            {
                if (img.ActualHeight < img.Source.Height * 5)
                {
                    double h2 = img.Height = img.ActualHeight * 1.1;
                    double w2 = img.Width = img.Source.Width * h2 / img.Source.Height;
                }
            }

            // PROBLEM HERE:

            else if (img.ActualHeight > 100) img.Height = img.ActualHeight / 1.1;
        }
    }

【问题讨论】:

    标签: wpf scrollviewer horizontalscrollview


    【解决方案1】:

    问题是您在缩小图像时没有设置Image 控件的Width 属性。图像控件实际上自动保持纵横比(Stretch 属性默认设置为Uniform)。但是,当图像调整大小时,控件 本身会保持您在放大期间设置的大小。另请注意,我已更正您的修饰键检查以使用 WPF 而不是 Windows 窗体:

    void sv_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
        {
            double? h2 = null;
            if (e.Delta > 0)
            {
                if (img.ActualHeight < img.Source.Height * 5)
                {
                    h2 = img.Height = img.ActualHeight * 1.1;
                }
            }
            else if (img.ActualHeight > 100)
            {
              h2 = img.Height = img.ActualHeight / 1.1;
            }
    
            if (h2 != null)
            {
                 img.Width = img.Source.Width * h2.Value / img.Source.Height;
            }
        }
    }
    

    另一种解决方案是使用变换来缩放图像。

    XAML

    <Image Name="img"
           HorizontalAlignment="Center"
           VerticalAlignment="Center">
        <Image.LayoutTransform>
            <ScaleTransform x:Name="imageScale" />
        <Image.LayoutTransform>
    </Image>
    

    C#

    void sv_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
        {
            e.Handled = true; // stops scrolling due to wheel
    
            if (e.Delta > 0)
            {
                if (imageScale.ScaleX < 5)
                {
                    imageScale.ScaleX *= 1.1;
                    imageScale.ScaleY *= 1.1;
                }
            }
            else
            {
                imageScale.ScaleX /= 1.1;
                imageScale.ScaleY /= 1.1;
            }
        }
    }
    

    您也可以使用其他变换,例如旋转。在MSDN 上阅读更多相关信息。

    【讨论】:

    • 另外,您应该考虑改用ScaleTransform。简单得多。
    • 我不知道如何使用它,我是 WPF 的新手,你能给我发一个与我上述任务相关的示例链接吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    相关资源
    最近更新 更多