0

当从以下层次结构中单击按钮时,我想访问每个 listboxitem 模板控件的(图像和 4 个文本块控件)数据。

 <ListBox x:Name="lstBoxNearbyPlaces" ItemsSource="{Binding Items}" Grid.Row="2" Margin="0,10,0,0"  >
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Button Width="400" Height="150" Name="btnPlace" Click="btnPlace_Click_1">
                                    <Button.Content>                                            
                                        <StackPanel Orientation="Horizontal" >                                              
                                            <Image  Name="ImgPlace"  Source="{Binding PlaceIconURL}" Width="50" />
                                            <TextBlock Name="lblPlaceLat" Text="{Binding PlaceLatLocation}" Visibility="Collapsed"/>
                                            <TextBlock Name="lblPlaceLng" Text="{Binding PlaceLongLocation}" Visibility="Collapsed"/>
                                            <StackPanel Width="350" >                                                    
                                                <TextBlock FontSize="20"   Text="{Binding PlaceName}" Name="txtAddress" TextWrapping="Wrap" ></TextBlock>                                        
                                                <TextBlock FontSize="20" Text="{Binding PlaceVicinity}" Name="txtLocation" TextWrapping="Wrap"></TextBlock>
                                            <Line MinHeight="5"></Line>
                                            </StackPanel>
                                        </StackPanel>
                                    </Button.Content>
                                </Button>
                            </DataTemplate>

                        </ListBox.ItemTemplate>
                    </ListBox>

触发按钮单击事件时,是否可以在此处获取或读取每个控件的数据?

4

1 回答 1

1

您可以在 ListBox_SelectionChanged 中获取绑定对象作为选中项。

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      ItemModel model= listBox.SelectedItem as ItemModel ;
    }

或者在 Buttonclick 事件中,您可以将其作为

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button button = sender as Button;
        ItemModel model = button.DataContext as ItemModel;
    }

这里的模型是您绑定到该模板的对象。

于 2013-02-25T04:33:10.277 回答