1

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?

4

3 回答 3

1

you need to set Width and Height for it (it will not work in Auto)

i.e.: <Canvas Width="250" Height="235">

tried on mind and it worked, good luck!

于 2011-04-08T13:15:02.530 回答
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;
}
于 2011-04-08T10:58:28.990 回答
0

As mentioned Marat, the Canvas does not change its size after its content. But we can force it like this, naming the parent Window as by eg."myWpfApplication4.MainWindow" and:

<Canvas
  Height="{Binding ElementName=myWpfApplication4.MainWindow, Path=ActualHeight}"
  Width="{Binding ElementName=myWpfApplication4.MainWindow, Path=ActualWidth}">
于 2011-04-08T13:12:40.827 回答