Шаблоны не применяются при прокрутке

Я использую сетку с прокруткой просмотра. Я разработал эту сетку, в которой больше не было элементов пользовательского интерфейса. Открыв этот отчет, я визуализировал только элементы пользовательского интерфейса первой страницы из-за проблемы с производительностью. Во время прокрутки необходимо отобразить оставшиеся элементы пользовательского интерфейса.

Мой вопрос

При прокрутке шаблоны просмотра с прокруткой не применяются в течение секунд оставшихся элементов. Но элементы пользовательского интерфейса первой страницы корректно обновляются с помощью шаблонов.

Почему при прокрутке шаблоны не применяются ни на секунду?

Фрагмент кода файла Xaml:

     <ScrollViewer Grid.Row="1" Focusable="False"  ScrollChanged="DesignScrollViewer_ScrollChanged" x:Name="DesignScrollViewer" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
            <ScrollViewer.Template>
                <ControlTemplate  TargetType="{x:Type ScrollViewer}">
                    <Grid x:Name="Grid" Background="#FFF9F9F9">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="0" />
                        </Grid.RowDefinitions>
                        <Grid AllowDrop="True" Grid.Column="0" Grid.Row="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <Border Background="#ffffffff" BorderBrush="#ffbbbbbb" Visibility="Collapsed" BorderThickness="0,1,1,0" Grid.Column="1">
                                <StackPanel Orientation="Horizontal">
                                    <ToggleButton Width="16" ToolTip="Layout" x:Name="AutoFit" Grid.Column="0" Style="{StaticResource ToggleButtonStyle}" Click="AutoFit_Click_1" VerticalAlignment="Stretch" Margin="0" Height="12">
                                        <Path x:Name="fittoscreen" Data="M8.84896,6.54523 L10.66668,6.54523 10.66668,10.666019 6.5462299,10.666019 6.5462299,8.847971 8.84896,8.847971 z M0,6.54523 L1.8183599,6.54523 1.8183599,8.8479717 4.12177,8.8479717 4.12177,10.66602 0,10.66602 z M6.5462299,0 L10.66668,0 10.66668,4.1210899 8.84896,4.1210899 8.84896,1.8183599 6.5462299,1.8183599 z M0,0 L4.12177,0 4.12177,1.8183599 1.8183601,1.8183599 1.8183601,4.1210899 0,4.1210899 z" Fill="#FF646464" Height="8" Stretch="Fill" Width="8" />
                                    </ToggleButton>
                                    <ComboBox Margin="0" ItemContainerStyle="{StaticResource ZoomComboboxItem}" AllowDrop="True" SelectedIndex="-1" Style="{StaticResource ZoomComboBoxStyle}" IsEditable="True" VerticalAlignment="Stretch" Height="12" FontSize="10" Tag="AtStart" PreviewKeyDown="cmboSearchField_PreviewKeyDown"  BorderThickness="0" x:Name="cmboSearchField"   Width="50" KeyDown="cmboSearchField_KeyDown_1" SelectionChanged="cmboSearchField_SelectionChanged_1" IsTextSearchEnabled="false" />

                                        <ComboBoxItem FontSize="10" x:Name="Item1" Content="20 %" />
                                        <ComboBoxItem FontSize="10" x:Name="Item2" Content="50 %" />
                                        <ComboBoxItem FontSize="10" x:Name="Item3" Content="70 %" />
                                        <ComboBoxItem FontSize="10" x:Name="Item4" Content="100 %" />
                                        <ComboBoxItem FontSize="10" x:Name="Item5" Content="150 %" />
                                        <ComboBoxItem FontSize="10" x:Name="Item6" Content="200 %" />
                                        <ComboBoxItem FontSize="10" x:Name="Item7" Content="400 %" />
                                    </ComboBox>
                                </StackPanel>
                            </Border>

                            <Border Background="#FFFFFFFF" VerticalAlignment="Bottom" BorderBrush="#FFBBBBBB" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" BorderThickness="0,1,1,0" Grid.Column="0">
                                <ScrollBar HorizontalAlignment="Stretch" BorderThickness="0" AllowDrop="True" x:Name="PART_HorizontalScrollBar" AutomationProperties.AutomationId="HorizontalScrollBar" Cursor="Arrow"  Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Orientation="Horizontal"  Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportWidth}"    Margin="0,-1" Height="16" />
                            </Border>
                        </Grid>
                        <!--<Rectangle x:Name="Corner" Grid.Column="" Fill="Transparent" Grid.Row="1" />-->
                        <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="0"  Grid.Row="0"/>
                        <Border  x:Name="ContentScroll" BorderThickness="0" Background="#FFF9F9F9" HorizontalAlignment="Right" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" BorderBrush="#FFBBBBBB" Grid.Column="1">
                            <ScrollBar  HorizontalAlignment="Right"  HorizontalContentAlignment="Center" x:Name="PART_VerticalScrollBar"  AutomationProperties.AutomationId="VerticalScrollBar" Cursor="Arrow" Grid.Column="1" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Grid.Row="0" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" ViewportSize="{TemplateBinding ViewportHeight}" />
                        </Border>
                    </Grid>
                </ControlTemplate>
            </ScrollViewer.Template>
            <Grid  Focusable="False" VerticalAlignment="Center"  HorizontalAlignment="Center">
                <!--<Grid.RowDefinitions>
                <RowDefinition x:Name="FilterZoneRow" Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>-->
                <!--<Grid  x:Name="FilterZone" Margin="0,0,0,5" Background="#FF1A1A1A" Visibility="Collapsed">
                <Grid  x:Name="FilterGrid">
                    <designLayout:GridCanvas x:Name="FZoneCanvas" KeyboardNavigation.IsTabStop="False" RowCount="0" IsInDesignMode="True" CanvasLayout="DesktopFreeForm"  FreeFormSize="Small"   Transform="{Binding ElementName=Canvas,Path=Transform,Mode=TwoWay}" ShowGridLines="{Binding Path=ShowGridLines,ElementName=Canvas}" Background="#FF1A1A1A">
                    </designLayout:GridCanvas>
                    <Canvas>
                        <Border x:Name="FilterControlPlacementBorder" Visibility="Collapsed" BorderBrush="Red" BorderThickness="1">
                        </Border>
                        <Button x:Name="FilterControlPlacementContent" KeyboardNavigation.IsTabStop="False"  Visibility="Collapsed" IsHitTestVisible="False" BorderBrush="{x:Null}" VerticalAlignment="Center" HorizontalAlignment="Center" />
                        <Rectangle StrokeDashArray="4 2" x:Name="SelectionFRect" Visibility="Collapsed" Stroke="Gray" StrokeThickness="1.5" />
                    </Canvas>
                </Grid>
                <Border x:Name="FilterZoneBorder" BorderBrush="Yellow" AllowDrop="False" Visibility="Collapsed" BorderThickness="1" />
            </Grid>-->
                <Grid Grid.Row="0" Focusable="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  FocusVisualStyle="{x:Null}"  x:Name="CanvasGrid">
                    <Border BorderBrush="Transparent" BorderThickness="0" Background="Transparent">
                        <Border.Effect>
                            <DropShadowEffect ShadowDepth="0" BlurRadius="5" Opacity="0.3" Color="Black" />
                        </Border.Effect>
                    </Border>

                        <designLayout:GridCanvas KeyboardNavigation.IsTabStop="True" KeyboardNavigation.TabNavigation="Once" FocusVisualStyle="{x:Null}"  Background="Transparent" x:Name="Canvas" CanvasLayout="DesktopGridCanvas"  VerticalAlignment="Center" HorizontalAlignment="Center"  ClipToBounds="True" Focusable="True"  ShowGridLines="True" IsInDesignMode="True">
                        </designLayout:GridCanvas>

                    <Canvas>
                        <Border x:Name="ControlPlacementBorder" Visibility="Collapsed"  BorderThickness="1">
                            <!--<Button x:Name="ControlPlacementContent" IsHitTestVisible="False" BorderBrush="{x:Null}" VerticalAlignment="Center" HorizontalAlignment="Center" />-->
                        </Border>
                    </Canvas>
                </Grid>
            </Grid>
        </ScrollViewer>

Код позади:

 private void DesignScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
        if (e.VerticalOffset > OffsetValue)
        {
            OffsetValue = e.VerticalOffset;
            if (TempCanvasRowCount < DesignCanvas.RowCount &&(e.VerticalOffset/48) > prevCanvasRowCount)
            {
                TempCanvasRowCount += 12;
                prevCanvasRowCount += 12;
                RenderingElements();
                if (TempCanvasRowCount > DesignCanvas.RowCount)
                    TempCanvasRowCount = DesignCanvas.RowCount;
            }
        }
    }

Заранее спасибо.

1 ответ

В заключение,

Я обнаружил, что решение для шаблонов не применяется при виртуальной прокрутке.

Запустите код рендеринга виджета, так как фоновый рабочий является решением для этого.

private void DesignScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
  if (e.VerticalOffset > OffsetValue)
   {
     OffsetValue = e.VerticalOffset;
     if (TempCanvasRowCount < DesignCanvas.RowCount && (e.VerticalOffset/48) > prevCanvasRowCount)
      {
        TempCanvasRowCount += 12;
        prevCanvasRowCount += 12;
        var _backGroundWorker = new BackgroundWorker { }
        _backGroundWorker.DoWork += (obj, args) =>
         {
           RenderingWidgets(window.DashboardDesigner);
          };
        _backGroundWorker.RunWorkerCompleted += (obj, args) =>
         {
            if (TempCanvasRowCount > DesignCanvas.RowCount)
                TempCanvasRowCount = DesignCanvas.RowCount; 
          };
                _backGroundWorker.RunWorkerAsync(this);             
        }
    }           
}
Другие вопросы по тегам