反正有没有访问此文本块?

时间:2012-07-18 23:34:52

标签: c# windows-phone-7 listbox parameter-passing textblock

我希望在绑定列表框内的“GameName”文本块后访问文本内容。

        <controls:PanoramaItem Header="games" Margin="0" Height="800" Foreground="White" VerticalAlignment="Center">
            <!--Double line list with text wrapping-->
            <ListBox x:Name="GamesListBox" Margin="0,0,-12,66"  Height="614" ItemsSource="{Binding dataFeed}" SelectionChanged="GamesListBox_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                            <!--Replace rectangle with image-->
                            <Image Height="100" Width="100"  Source="{Binding AllGamesImage}" Margin="12,0,9,0" VerticalAlignment="Top" HorizontalAlignment="Left" Stretch="UniformToFill" />
                            <StackPanel Width="311">
                                <TextBlock x:Name="GameName" Text="{Binding AllGamesTitle}" TextWrapping="Wrap" Foreground="White" Style="{StaticResource PhoneTextExtraLargeStyle}" Margin="0,0,0,1"/>
                                <TextBlock Text="{Binding AllGamesDescription}" TextWrapping="Wrap" Margin="0,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </controls:PanoramaItem>

我想把所设置的任何内容作为文本内容,并将其作为参数或任何内容传递给另一个页面。

在Shawn Kendrot建议之后,代码落后了。

    private void GamesListBox_SelectionChanged(object sender, EventArgs e)
    {
        var myObject = GamesListBox.SelectedItem as NewGamesClass;
        string gameName = myObject.TitleCode;

        NavigationService.Navigate(new Uri("/Pages/AchivementListPage.xaml?gameName=" + gameName, UriKind.Relative));
    }

我在这里回到页面时得到一个NullExeception:

string gameName = myObject.TitleCode;

2 个答案:

答案 0 :(得分:3)

为什么在可以访问对象的属性时访问Text属性?

void GamesListBox_SelectionChanged(object sender, EventArgs e)
{
    var myObject = GamesListBox.SelectedItem as MyObject;
    string gameName = myObject.AllGamesTitle;
    // Do something with gameName
}

答案 1 :(得分:0)

试试这个

ListBoxItem selItem = (ListBoxItem)(listboxWeight.ItemContainerGenerator.ContainerFromIndex(listboxWeight.SelectedIndex));

StackPanel weightpanel = (StackPanel)selItem.Content;

var child1 = weightpanel.Children[0] as TextBlock;
var child2 = weightpanel.Children[1] as TextBlock;