滚动查看器的WPF Horizo​​natl滚动条未显示完整的Canvas视图

时间:2019-01-24 09:22:16

标签: c# wpf canvas data-binding scrollviewer

我在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看到绘制的新网格线。我知道有内容。但我无法接受。滚动查看器没有显示它。我尝试使用Layouttransform而不是RenderTransform。这给了我难看的输出。使用RenderTransform进行缩放非常完美。唯一的问题是我没有获得画布的所有内容。我也尝试通过从Canvas Class派生它来使主要的外部画布成为自定义Canvas。仍然无法正常工作。我还尝试将ScrollViewer的宽度绑定到父网格ActualWidth。但是产量没有变化。我将分享发布结果截图的两个链接。 我感谢所有抽出时间帮助我的人。

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

<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>

0 个答案:

没有答案