如何保持列表框中屏幕外的最后一项

时间:2012-08-23 22:45:21

标签: windows-phone-7

我在MainPage.xaml上使用了一个列表框。通过使用鼠标上下移动,我可以看到内容在Windows Phone Emulator屏幕上消失。释放鼠标后,内容将从第一个项目开始。假设我想选择列表框中的最后一项。如何保留屏幕上的最后一项?希望你能帮助我或给我一个例子或想法来解决这个问题。提前谢谢。

修改

有我的用户控件:

<UserControl x:Class="CMSPhoneApp.QueueListControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"
xmlns:local="clr-namespace:CMSPhoneApp"  >

<UserControl.Resources>       
    <local:VisibilityConverter x:Key="VisibilityConverter"/>
    <local:ColumSpanConverter x:Key="ColumSpanConverter"/>        
</UserControl.Resources>


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

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">       
            <ListBox x:Name="lst" HorizontalAlignment="Left" Margin="6,6,0,0"   VerticalAlignment="Top" Width="400"    
                  ItemsSource="{Binding Path=MyQueue}"  
                      SelectedItem="{Binding Path=CurrentQueue, Mode=TwoWay}"  >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderThickness="1,1,1,1" BorderBrush="Blue">
                            <Grid Width="auto" HorizontalAlignment="Stretch" >                                  
                                    <Grid.RowDefinitions>                                          
                                    <RowDefinition Height="auto"/>
                                </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="35" />
                                        <ColumnDefinition Width="250"/>
                                        <ColumnDefinition Width="125" />
                                    </Grid.ColumnDefinitions>
                                    <Image Source="{Binding Type}" Grid.Row="0" Grid.Column="0"/>
                                    <TextBlock  Grid.Row="0" Grid.Column="1"  Grid.ColumnSpan= "{Binding isSpan, Converter={StaticResource ColumSpanConverter}}" Text="{Binding summary}" TextWrapping="Wrap"
                                             Style="{StaticResource PhoneTextAccentStyle}"  />
                                    <Button x:Name="btnAction" Grid.Row="0" Grid.Column="3" ClickMode="Press" Click="btnAction_Click" Style="{StaticResource ButtonStyle1}"
                                            Visibility="{Binding isVisibility, Converter={StaticResource VisibilityConverter}}"
                                         Tag="{Binding callNumber}">

                                        <Button.Content>
                                            <TextBlock Width="85" Height="70" Text="{Binding ActionCaption}" 
                                                       Style="{StaticResource LabelStyle_20}"     />
                                        </Button.Content>
                                    </Button>

                                </Grid>
                                </Border>
                    </DataTemplate>                    

                </ListBox.ItemTemplate>
            </ListBox>

    </Grid>
</Grid>

xaml页面使用上述用户控件。

   <phone:PhoneApplicationPage xmlns:my="clr-namespace:CMSPhoneApp"  
x:Class="CMSPhoneApp.TestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"
xmlns:local="clr-namespace:CMSPhoneApp"
  xmlns:toolkit="clr- namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
  >

<phone:PhoneApplicationPage.Resources>

    <local:QueueItemViewModel x:Key="TestViewModel"/>
    <DataTemplate x:Name="PickerItemTemplate">
        <StackPanel Orientation="Horizontal" >
            <TextBlock Text="{Binding city}" FontSize="24" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </StackPanel>
    </DataTemplate>
    <DataTemplate x:Name="PickerFullModeItemTemplate">
        <StackPanel Orientation="Horizontal" >
            <TextBlock Text="{Binding city}" Margin="16 0 0 0" FontSize="24" FontFamily="{StaticResource PhoneFontFamilyLight}" />                        
        </StackPanel>
    </DataTemplate>


</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{StaticResource TestViewModel}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>           
    </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="Test page" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <StackPanel x:Name="City" Grid.Row="1" Margin="12,0,12,0"  Orientation="Horizontal">
        <toolkit:ListPicker   x:Name="listPicker" ItemTemplate="{StaticResource PickerItemTemplate}"  
              Header="Location"   CacheMode="BitmapCache" Width="194" 
                        SelectedItem="{Binding city}"        />

    </StackPanel>
    <!--ContentPanel - place additional content here-->       
        <my:QueueListControl x:Name="lst" Grid.Row="2"/>     
    <StackPanel Grid.Row="3" Orientation="Horizontal" >
        <Button x:Name="btnQueue" Style="{StaticResource ButtonStyle1}" Content="Queue"/>
        <Button x:Name="btnRefresh" Style="{StaticResource ButtonStyle1}" Content="Refresh"/>
        </StackPanel>

</Grid>

获取GetResponseStream后设置itemSource的代码:

 Dispatcher.BeginInvoke(new Action(() => lst.lst.ItemsSource = ar.cmsQueue));   

2 个答案:

答案 0 :(得分:1)

你有。您没有设置列表框的高度,因此它从其父级获得高度,即Auto(这意味着ListBox的高度没有边界)。因此,通过按住鼠标,您可以滚动到任何项目,但不能停在特定项目。将高度修复到大约400左右的ListBox,然后检查。希望这应该可以解决你的问题

答案 1 :(得分:0)

你在代码的任何地方做了类似的事吗?

listbox.ScrollIntoView(0);,其中listbox是mainPage.xaml上ListBox的名称

尽量避免使用这样的代码行。除此之外,如果没有一些代码,我无法理解你的问题的本质。希望它有所帮助