1

我必须将元素插入列表顶部的项目控件。在第 0 个位置向列表中插入元素时,列表正在滚动到该项目。但是我不想在插入元素时将项目滚动到列表顶部。使这成为可能的任何想法。

xaml 代码:

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

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
        <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        <Button Height="72" Content="Add More" Click="Button_Click"/>
    </StackPanel>

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

        <ScrollViewer x:Name="MainScrollViewer" Grid.Row="1" Margin="12,0,12,0" >
        <Grid x:Name="ContentPanel" >
            <ItemsControl x:Name="MainItemsControl" ItemsSource="{Binding}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Margin="10" Text="{Binding Item}" FontSize="28" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </ScrollViewer>
    </Grid>

我的班级代码:

 public partial class MainPage : PhoneApplicationPage
  {
    private ObservableCollection<Model> mainList;
    public ObservableCollection<Model> MainList
    {
        get { return mainList; }
        set
        {
            mainList = value;
        }
    }

    public MainPage()
    {
        InitializeComponent();
    }

    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        MainList = new ObservableCollection<Model>();
        AddItems(15);
        ContentPanel.DataContext = MainList;  
    }       

    void AddItems(int numberIfItemsToAdd)
    {
        Debug.WriteLine("AddItems()");
        int start = MainList.Count;
        int end = start + numberIfItemsToAdd;
        for (int i = start; i < end; i++)
        {
            Model model = new Model();
            model.Item="Item" + i;
           MainList.Insert(0, model);
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        AddItems(1);
    }
}

先感谢您。

4

0 回答 0