WPF:ScrollBer上没有激活ScrollBar

时间:2011-04-08 10:22:34

标签: .net wpf scaletransform

WPF
为什么放大时ScrollViewer上没有激活ScrollBar?

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="356" Width="804">
    <Grid>
        <ScrollViewer
            PreviewMouseWheel="ScrollViewer_PreviewMouseWheel" 
            VerticalScrollBarVisibility="Visible"
            HorizontalScrollBarVisibility="Visible">
            <Canvas>
                <Canvas.LayoutTransform>
                    <TransformGroup>
                        <ScaleTransform x:Name="scaleTransform"/>
                    </TransformGroup>
                </Canvas.LayoutTransform>
                <TextBlock Canvas.Left="34" Canvas.Top="47" Name="textBlock1" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; BLA BLA BLA...... " />
                <TextBlock Canvas.Left="310" Canvas.Top="46" Name="textBlock4" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; BLA BLA BLA...... " />
                <TextBlock Canvas.Left="188" Canvas.Top="157" Name="textBlock11" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; BLA BLA BLA...... " />
            </Canvas>
        </ScrollViewer>
    </Grid>
</Window>

CS:

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    if (e.Delta > 0)
        scaleTransform.ScaleY =
            scaleTransform.ScaleX += 0.1;
    else
        scaleTransform.ScaleY =
            scaleTransform.ScaleX -= 0.1;
}

为什么放大时没有激活ScrollBar?

3 个答案:

答案 0 :(得分:1)

Canvas不会调整其内容的大小。你应该使用另一个面板。网格例如:

<ScrollViewer
    PreviewMouseWheel="ScrollViewer_PreviewMouseWheel" 
    VerticalScrollBarVisibility="Visible"
    HorizontalScrollBarVisibility="Visible">
    <Grid>
        <Grid.LayoutTransform>
            <TransformGroup>
                <ScaleTransform x:Name="scaleTransform"/>
            </TransformGroup>
        </Grid.LayoutTransform>
        <TextBlock Margin="34,47" Name="textBlock1" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; BLA BLA BLA...... " />
        <TextBlock Margin="310,46" Name="textBlock4" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; BLA BLA BLA...... " />
        <TextBlock Margin="188,157" Name="textBlock11" Text="TK QSDFWPO Aàâéèêëîïôùûüÿçæœ; BLA BLA BLA...... " />
    </Grid>
</ScrollViewer>

我认为您需要将事件标记为已处理以防止垂直滚动:

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    if (e.Delta > 0)
        scaleTransform.ScaleY =
            scaleTransform.ScaleX += 0.1;
    else
        scaleTransform.ScaleY =
            scaleTransform.ScaleX -= 0.1;

    e.Handled = true;
}

答案 1 :(得分:1)

你需要为它设置宽度和高度(它在自动中不起作用)

即: <Canvas Width="250" Height="235">

尝试过,它运作良好,祝你好运!

答案 2 :(得分:0)

正如Marat所说,Canvas的内容并没有改变它的大小。 但我们可以像这样强制它,命名父窗口,例如“myWpfApplication4.MainWindow”和:

<Canvas
  Height="{Binding ElementName=myWpfApplication4.MainWindow, Path=ActualHeight}"
  Width="{Binding ElementName=myWpfApplication4.MainWindow, Path=ActualWidth}">