Windows phone 7 如何在发生listboxitem模板数据的事件时访问它';s的项目控制被激发?
当从以下层次单击按钮时,我想访问每个listboxitem模板控件(图像和4个textblock控件)的数据Windows phone 7 如何在发生listboxitem模板数据的事件时访问它';s的项目控制被激发?,windows-phone-7,windows-phone-7.1,windows-phone-8,Windows Phone 7,Windows Phone 7.1,Windows Phone 8,当从以下层次单击按钮时,我想访问每个listboxitem模板控件(图像和4个textblock控件)的数据 <ListBox x:Name="lstBoxNearbyPlaces" ItemsSource="{Binding Items}" Grid.Row="2" Margin="0,10,0,0" > <ListBox.ItemTemplate> <Da
<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>
触发按钮单击事件时,是否可以在此处获取或读取每个控件的数据?您可以将列表框中的绑定对象更改为所选项目
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;
}
此处模型是您绑定到该模板的对象。使用按钮
命令绑定
这可能会帮助您[1]: