C# 选定的列表框项目位于wp8的中心

C# 选定的列表框项目位于wp8的中心,c#,windows-phone-8,C#,Windows Phone 8,我在WP8中水平显示列表框中的动态数据。但我需要将所选项目显示在中心。如何做到这一点 请查看我尝试的代码: private void wifiListboxItems_SelectionChanged_1(object sender, SelectionChangedEventArgs e) { HomePageClass itm = wifiListboxItems.SelectedItem as HomePageClass; tblMiddletext.Text = itm

我在WP8中水平显示
列表框中的动态数据。但我需要将所选项目显示在中心。如何做到这一点

请查看我尝试的代码:

private void wifiListboxItems_SelectionChanged_1(object sender, SelectionChangedEventArgs e) 
{
    HomePageClass itm = wifiListboxItems.SelectedItem as HomePageClass;
    tblMiddletext.Text = itm.Name1.ToString();

    int itemIndex = wifiListboxItems.Items.IndexOf(wifiListboxItems.SelectedItem);
    double topIndex = (int)(this.viewer.HorizontalOffset - Math.Truncate(viewer.ViewportWidth / 2));
    bool atLastItem = topIndex >= 233 - viewer.HorizontalOffset / itemIndex;

    if (itemIndex == wifiListboxItems.Items.Count - 1)
    {
        atLastItem = false;
    }
    if (atLastItem)
    {
        double dd = topIndex + 100;
        viewer.ScrollToHorizontalOffset(dd);
    }
    else
    {
        double dd = topIndex + 390 - itemIndex;
        viewer.ScrollToHorizontalOffset(dd);
    }

    wifiListboxItems.SelectedItem = -1;
}
但这是行不通的

更新:Mainpage.Xaml

<ListBox  Name="wifiListboxItems" Height="350" Width="467" Canvas.Left="4"        HorizontalContentAlignment="Center" SelectionChanged="wifiListboxItems_SelectionChanged_1"  ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Disabled">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate >
            <wptoolkit:WrapPanel  Margin="160,0,150,0" HorizontalAlignment="Left">
            </wptoolkit:WrapPanel>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Name="stackpanle" Loaded="stackpanle_Loaded"  Orientation="Horizontal" Height="auto" Width="auto">
                <Grid Name="gridim">
                    <Border Height="{Binding Name,Converter={StaticResource HeightConverterTest}}" Width="127" Margin="5" VerticalAlignment="Top" Background="#96345a" CornerRadius="0,0,80,80"/>
                    <Border Height="80" Width="80" Margin="0,40,0,50" VerticalAlignment="Bottom" Background="White" CornerRadius="50">
                        <Image  Name="imgicon"  Height="50" Width="50" Stretch="Fill" HorizontalAlignment="Center" Margin="15,0,15,16" VerticalAlignment="Bottom" Source="{Binding AppIcon}"/>
                    </Border>
                    <TextBlock Name="tblPer" Text="{Binding Name}" HorizontalAlignment="Right" Margin="-10,10,62,0" VerticalAlignment="Top" ></TextBlock>
                    <TextBlock Name="tblPersymbol" Text="%" HorizontalAlignment="Right" Margin="0,10,40,0" VerticalAlignment="Top" ></TextBlock>
                </Grid>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>


嗨,我在这里看到了你的新手机。我建议您更新您的问题,并让我们知道您到目前为止尝试了什么。您好BlackM,请查看我上面的更新。是否要在文本框(即TBLMidletext)中集中显示所选项目文本?不,我需要在列表框中显示所选项目以使其位于中心。@CoryCharlton…很抱歉,我没有理解您。您能进一步解释吗?