使用ScaleTransform缩放画布时,ScrollViewer不会显示所有内容

时间:2019-01-21 14:57:55

标签: c# wpf canvas scrollviewer scaletransform

我在ScrollViewer中有两个Canvas面板。一种是主画布,其背面绘制有网格形状。然后,我有两个ItemsControl。第一个ItemsControl将Stackpanel作为具有水平方向的ItemsPanel。第二个ItemsControl将Canvas作为其面板。我在此画布上在Itemscontrol的DataTemplate中绘制Line对象。此画布上有PreviewMouseWheel事件。在事件处理程序中,我正在缩放此画布,该画布正在缩放Line对象。该画布的宽度绑定到ViewModel属性CanvasWidth。此外,这还将更改“外部画布”的宽度,因为其宽度也绑定到ViewModel属性CanvasWidth。触发PreviewMouseWheel后,我在主Canvas上添加了更多网格线。我有TextBlock作为ItemsSource的DataTemplate。在放宽之前,最后一个TextBlock的内容为14260。在放大之后,它应保留为14260。但应减小两个连续TextBlock的步长值。现在,我无法通过ScrollViewer看到全部内容。减小了所需的步长,但无法通过Scrollviewer看到绘制的新网格线。我知道有内容。但我无法接受。滚动查看器没有显示它。

    <Grid x:Name="grid1" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="20" /> 
                    <RowDefinition Height="*" /> 
                    <RowDefinition Height="20" /> 
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="20" /> 
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*"/>   
                    <ColumnDefinition Width="20"/> 
                </Grid.ColumnDefinitions>
         <ScrollViewer Name="scrollViewer" HorizontalScrollBarVisibility="Auto" Grid.Row="1" Grid.Column="3"   Margin="10,10,0,10" >
                        <Canvas Name="back_canvas"  Height="12000"  Width="{Binding CanvasWidth}" Margin="0,0,10,0" >
                            <Canvas.Background>
                                <DrawingBrush TileMode="Tile" Viewport="0,0,40,40"  ViewportUnits="Absolute"> 
                                    <DrawingBrush.Drawing>
                                        <GeometryDrawing>
                                            <GeometryDrawing.Geometry>
                                                <RectangleGeometry Rect="0,0,50,50"/>
                                            </GeometryDrawing.Geometry>
                                            <GeometryDrawing.Pen>
                                                <Pen Brush="Gray" Thickness="1"/>
                                            </GeometryDrawing.Pen>
                                        </GeometryDrawing>
                                    </DrawingBrush.Drawing>
                                </DrawingBrush>
                            </Canvas.Background>


                            <ItemsControl ItemsSource="{Binding TimeAxis}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding}"  Margin="0,0,3,0"    Width="37"  Background="GreenYellow" >
                                        </TextBlock>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        <ItemsControl ItemsSource="{Binding Lines}">
                            <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <Canvas Height="12000" Background="Transparent"  Name="front_canvas" 
                                            PreviewMouseWheel="OnPreviewMouseWheel"
                                              Width="{Binding CanvasWidth, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        </Line>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                        </ItemsControl>
         </Canvas>
         </ScrollViewer>
 </Grid>

 private void UpdateGraph(Canvas canvas, double deltaValue)
        {
            List<MarkerView> markers = new List<MarkerView>();
            scaleFactor += deltaValue;

            double tempScale = scaleFactor;

            if (scaleFactor < 1.0)
            {
                scaleFactor = 1.0;
            }
            if (scaleFactor > maximumScale)
            {
                scaleFactor = maximumScale;
            }

            if (tempScale > 0)
            {
                totalSamples = graphVM.maxSignalLength;

                maximumCanvasWidth = totalSamples * maximumDeltaDistance;
                if(scaleFactor<=maximumDeltaDistance)
                {
                ScaleTransform scaleTransform = new ScaleTransform(scaleFactor, 1);
                canvas.RenderTransform = scaleTransform;
                verticalLines.ForEach(x =>
                {
                    x.RenderTransformOrigin = new Point(1, 1);

                    x.RenderTransform = new ScaleTransform(1 / scaleTransform.ScaleX, 1 / scaleTransform.ScaleY);
                });

                if (deltaValue < 0)
                {
                    graphVM.CanvasWidth = graphVM.InitialCanvasWidth * tempScale;
                }
                else
                {
                    if (graphVM.InitialCanvasWidth * scaleFactor > maximumCanvasWidth)
                        graphVM.CanvasWidth = maximumCanvasWidth;
                    else
                    graphVM.CanvasWidth = graphVM.InitialCanvasWidth * scaleFactor;
                }
                    graphVM.ResetLabels();

                   DeltaDistance = canvas.Width / totalSamples;
                MarkerView markerRed =
                UIHelperView.FindChild<MarkerView>(Application.Current.MainWindow, "splitterRed");
                MarkerView markerGreen =
                UIHelperView.FindChild<MarkerView>(Application.Current.MainWindow, "splitterGreen");
                markers.Add(markerRed);
                markers.Add(markerGreen);

                // Move  Markers with  zooming
                foreach (MarkerView marker in markers)
                {
                        marker.Delta = DeltaDistance; // after zooming if you move the marker then this value will be used to get correct position 
                        Canvas.SetLeft(marker, marker.XPosition * DeltaDistance);
                }

                    markers.Clear();

                }
            }

        }

这是输出图片https://imgur.com/a/7WTrBoc 这是放大的输出https://imgur.com/C7SCOSJ

1 个答案:

答案 0 :(得分:1)

RenderTransform不会影响控件的ActualWidth / Height。尝试改用LayoutTransform。