我在WP8中水平ListBox
显示动态数据。但是我需要将所选项目显示在中心位置。怎么做?
请参阅我尝试的代码:
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>