如何在代码后面的列表框中恢复文本块值?

时间:2013-03-24 14:20:56

标签: c# .net windows-phone-8

我会在

背后的代码中恢复文本块中的ItemId
<phone:PanoramaItem Header="Available">
            <Grid>
                <ListBox x:Name="ItemsCheckedinList" HorizontalAlignment="Left">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <toolkit:WrapPanel/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <Button x:Name="ButtonItemCheckedin" 
                Click="Button_ItemDetail" BorderBrush="{x:Null}" Width="200" 
                Height="200" Padding="0" Margin="0">
                                    <Button.Background>
                                        <ImageBrush ImageSource="{Binding Path=Image}" 
                Stretch="UniformToFill"/>
                                    </Button.Background>
                                    <StackPanel Orientation="Vertical" 
                Background="#FF0081C6" VerticalAlignment="Bottom" >
                                        <TextBlock Text="{Binding Path=Id}" 
                x:Name="ItemId" Visibility="Collapsed"/>
                                        <TextBlock Text="{Binding Path=Name}"/>
                                        <TextBlock Text="{Binding Path=Status}"/>
                                    </StackPanel>
                                </Button>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>
        </phone:PanoramaItem>

1 个答案:

答案 0 :(得分:1)

你真正想做的事情可能是从最初来自id的模型中获取价值。假设列表中包含的类名为Item,也许是这样的?

    private void Button_ItemDetail(object sender, RoutedEventArgs e)
        var fe = (FrameworkElement)sender;
        var item = (Item)fe.DataContext;
        var id = item.Id; // <-------------- id is in the variable id
    }

你也可以这样做:

    private void Button_ItemDetail(object sender, RoutedEventArgs e)
    {
        Button b = (Button)sender;
        var tb = (TextBlock)b.FindName("IdTextBox");
        var id = tb.Text; // <-------------- id is in the variable id
    }