0

我想在绑定列表框中的“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:

字符串游戏名称 = myObject.TitleCode;

4

2 回答 2

3

既然可以访问对象的属性,为什么还要访问 Text 属性?

void GamesListBox_SelectionChanged(object sender, EventArgs e)
{
    var myObject = GamesListBox.SelectedItem as MyObject;
    string gameName = myObject.AllGamesTitle;
    // Do something with gameName
}
于 2012-07-18T23:53:12.607 回答
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;
于 2012-07-19T05:30:39.720 回答