访问列表框中从用户控件中选择项目windows phone 7



我需要从用户控件访问列表框中选定的项目。我怎么能做到呢?例如,如果我有ListBox x:Name="NewPicturesListBox"在MainPage.xaml

!--Panorama item New one-->
            <controls:PanoramaItem Header="New one"  Name="Pan1" >
                <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
                    <ListBox Margin="0,0,-12,0" x:Name="NewPicturesListBox"

我需要从DetailControl.xaml.cs访问它们下面的代码可以在MainPage中给出结果。Xaml,其他页面呢,如果存在的话?

                if (NewPicturesListBox.ItemContainerGenerator != null)
                {
                    var contextMenuListItem = (ListBoxItem)(NewPicturesListBox.ItemContainerGenerator.ContainerFromItem(((MenuItem)sender).DataContext));
                    if (contextMenuListItem != null)
                    {
                        var selectedPicture = (contextMenuListItem).Content as Picture;

你可以在你的ViewModel和Property中,我们把它叫做selectedPicture,它将保存selectedValue,并从你的XAML绑定到它:

   <ComboBox Name="NewPicturesListBox" MinWidth="100" VerticalAlignment="Center" 
                                  ItemsSource="{Binding ListOfPictures}" 
                                  DisplayMemberPath="Name"
                                  SelectedValue ="{Binding Path=SelectedPicture}"  Width="141" />

最新更新