C# ItemsControl ScrollViewer在滚动后返回初始位置

C# ItemsControl ScrollViewer在滚动后返回初始位置,c#,xaml,windows-phone-8,itemscontrol,scrollviewer,C#,Xaml,Windows Phone 8,Itemscontrol,Scrollviewer,我正在向ItemsControl添加一个ScrollViewer,它的itemstemplate是动态生成的 问题在于ScrollViewer在滚动后会恢复到其初始位置。我尝试设置网格容器grid.Row=“1”,其中有ScrollViewer,使其具有足够的高度,或者将其设置为自动,但问题仍然存在。我错过了什么 <Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions>

我正在向
ItemsControl
添加一个
ScrollViewer
,它的
itemstemplate
是动态生成的

问题在于
ScrollViewer
在滚动后会恢复到其初始位置。我尝试设置网格容器
grid.Row=“1”
,其中有
ScrollViewer
,使其具有足够的高度,或者将其设置为自动,但问题仍然存在。我错过了什么

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel Grid.Row="0" Margin="12,17,0,28">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding GameLevel, Converter={StaticResource EnumToStringConverter}}" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock  HorizontalAlignment="Right" Grid.Column="1" Style="{StaticResource PhoneTextNormalStyle}">
                <Run Text="GAME "/>
                <Run Text="{Binding CurrentGame}"/>
                <Run Text=" / "/>
                <Run Text="{Binding TotalGame}"/>
            </TextBlock>
        </Grid>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle2Style}">
                <Run Text="{Binding CurrentTime, Converter={StaticResource SecondsToMinutesHour}}"/>   
            </TextBlock>
            <TextBlock Foreground="Green" HorizontalAlignment="Right" Grid.Column="1" Margin="9,-7,9,0" Style="{StaticResource PhoneTextTitle2Style}">
                <Run Text="{Binding TotalTime, Converter={StaticResource SecondsToMinutesHour}}"/>
            </TextBlock>

            <ProgressBar Grid.Row="1" Grid.ColumnSpan="2" x:Name="ProgressBar" Value="{Binding ProgressBarTime}" Minimum="0" Maximum="100" VerticalAlignment="Top" CacheMode="BitmapCache"/>
        </Grid>
    </StackPanel>

    <!--ContentPanel - place additional content here-->

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ScrollViewer>
        <ItemsControl x:Name="itemsControl" ItemsSource="{Binding Tiles}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Tag="{Binding Index}" Loaded="FrameworkElement_OnLoaded">
                        <Button Width="{Binding Side}" 
                               Height="{Binding Side}" 
                               Background="{Binding BgColor}"                                   
                               Tag="{Binding Index}"
                            Content="{Binding Index}"
                            FontSize="{StaticResource PhoneFontSizeSmall}"
                               Click="Button_Click">

                            <ia:Interaction.Triggers>
                                <ia:EventTrigger EventName="Loaded">
                                    <tr:SetCanvasPropertiesAction Left="{Binding Left}" Top="{Binding Top}" />
                                </ia:EventTrigger>
                            </ia:Interaction.Triggers>                               
                        </Button>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
        </ScrollViewer>         
    </Grid>

    <Grid x:Name="adGrid" Grid.Row="2" HorizontalAlignment="Center"/>
</Grid>

编辑: 好的,真正的问题是ItemsPNALTemplate是一个画布。画布的高度始终为0,宽度始终为0。放置在画布中的项目可以更大,但仍在画布中。scrollviewer只有画布的高度作为它应该能够滚动多少的概念。 您可以通过临时设置画布的高度来轻松测试这一点:

<ItemsPanelTemplate>
   <Canvas Height="1500"/>
</ItemsPanelTemplate>

你会看到,现在你可以滚动了

不幸的是,我现在想不出解决办法。(可能通过计算显示所有项目所需的宽度和高度,通过将(顶部+侧面)值最高的项目作为<代码>高度<代码>和(左侧+侧面)值作为<代码>宽度<代码>,通过代码隐藏来设置<代码>项目控制的宽度和高度。)

原件:

您的ContentPanel
Grid
具有此
Height=“Auto”
。因此,ContentPanel与ScrollViewer的高度相同,它的所有内容和您仍然可以执行的实际滚动只是ScrollViewer在滚动能力结束时的弹跳效果


只需删除
Height=“Auto”
就可以了。

之前代码中没有
Height=“Auto”
,因此无法工作。我再次删除它以确认它仍然不起作用。你是对的。在你编辑答案之前我就知道了。我通过绑定画布高度来修复它
<代码>画布高度通过添加项目的顶部+侧面来计算。现在,scrollviewer可以根据需要工作。