C# 将ListPicker中的selectedindex传递到另一页
当我通过Listpicker添加图书类型时,它是成功的,但是当我在Listbox中选择图书标题以显示图书的详细信息时,类型的值不会通过。我想要的是详细页面,类型显示在Listpicker中(如添加页面),并显示我以前选择的类型 Addpage.xamlC# 将ListPicker中的selectedindex传递到另一页,c#,windows-phone,parameter-passing,listpicker,C#,Windows Phone,Parameter Passing,Listpicker,当我通过Listpicker添加图书类型时,它是成功的,但是当我在Listbox中选择图书标题以显示图书的详细信息时,类型的值不会通过。我想要的是详细页面,类型显示在Listpicker中(如添加页面),并显示我以前选择的类型 Addpage.xaml <phone:PhoneApplicationPage.Resources> <DataTemplate x:Name="listpickertemplate"> <StackPanel Or
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Name="listpickertemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Genre}" Margin="10 0 0 0" />
</StackPanel>
</DataTemplate>
以及Bookdetails.xaml:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >
<ListBox x:Name="BookDetails">
<TextBlock x:Name="booktitle" Text="Book Title:" Foreground="#FF3B12AA" FontSize="30" FontWeight="Bold" FontFamily="Courier New"/>
<TextBox x:Name="booktitletext" Text="{Binding BookTitle, Mode=TwoWay}" Width="460" TextWrapping="Wrap" Background="#BF22A1DC" BorderBrush="#BFFFFFFF" FontFamily="Tahoma" FontSize="26"/>
<TextBlock x:Name="author" Text="Author:" Foreground="#FF3B12AA" FontSize="30" FontWeight="Bold" FontFamily="Courier New"/>
<TextBox x:Name="authortext" Tap="authortext_Tap" Text="{Binding Author, Mode=TwoWay}" Width="460" TextWrapping="Wrap" Background="#BF22A1DC" BorderBrush="#BFFFFFFF" FontFamily="Tahoma" FontSize="26"/>
<TextBlock x:Name="genre" Text="Genre:" Foreground="#FF3B12AA" FontSize="30" FontWeight="Bold" FontFamily="Courier New"/>
<toolkit:ListPicker x:Name="ListPicker" ItemTemplate="{StaticResource listpickertemplate}" Width="120" HorizontalAlignment="Left" Background="#BF3BDC22" SelectedIndex="{Binding Index}"/>
</ListBox>
</Grid>
我尝试模拟它,但当我选择booktileSystem时它显示错误。InvalidOperationException:SelectedIndex必须始终设置为有效值。
在Bookdetails页面的this.DataContext=App.MainViewModel
我猜,问题是您的代码在填充ListPicker的ItemsSource之前设置了DataContext(这意味着也设置了ListPicker的选定索引)。这将导致选择ListPicker的一个不存在的项目。在设置DataContext之前,尝试对代码重新排序以填充ListPicker:
public BookDetail()
{
InitializeComponent();
List<GenrePicker> newpicker = new List<GenrePicker>();
newpicker.Add(new GenrePicker() { Genre = "Comedy",Index = 0});
newpicker.Add(new GenrePicker() { Genre = "Science", Index = 1 });
newpicker.Add(new GenrePicker() { Genre = "Action", Index = 2});
this.ListPicker.ItemsSource = newpicker;
this.DataContext = App.MainViewModel;
}
public BookDetail()
{
初始化组件();
List newpicker=newlist();
Add(newgenrepicker(){Genre=“Comedy”,Index=0});
Add(newgenrepicker(){Genre=“Science”,Index=1});
Add(newgenrepicker(){Genre=“Action”,Index=2});
this.ListPicker.ItemsSource=newpicker;
this.DataContext=App.MainViewModel;
}
请尝试SelectedItem=null
而不是SelectedIndex=-1
,这样更有意义
当您设置SelectedIndex=-1
时,您会告诉列表选择器,“嘿,找到列表中的第-1个元素并将其设置为选中。”然后,列表选择器崩溃,因为它不理解ListPicker[-1]
的含义
<Grid x:Name="ContentPanel1" Margin="12,0,12,0">
<ListBox x:Name="TitleList" SelectionChanged="TitleList_SelectionChanged" ItemsSource="{Binding Load0}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="466" Margin="0, 0, 0, 12">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="360"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0"></Grid>
<StackPanel Grid.Column="1">
<TextBlock FontSize="40" Text="{Binding BookTitle}" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap"/>
</StackPanel>
<Grid Grid.Column="2">
<Button x:Name="Deletebutton" Height="50" Width="50" Click="deleteButton_Click" BorderBrush="{StaticResource TransparentBrush}" Margin="-40">
<Image Source="/Assets/delete.dark.png" Height="50" Width="50" Visibility="{StaticResource PhoneDarkThemeVisibility}" Margin="-40" />
</Button>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
private void TitleList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If no Book is selected, just return
if (TitleList.SelectedIndex== -1) return;
// Get the parent application that contains the Book being edited
App thisApp = Application.Current as App;
// Set this to the selected customer
thisApp.SelectedBook = TitleList.SelectedItem as Book;
thisApp.SelectedGenre = TitleList.SelectedItem as GenrePicker;
// Navigate to the detail page
NavigationService.Navigate(new Uri("/View/DetailPage/BookDetail.xaml",UriKind.RelativeOrAbsolute));
TitleList.SelectedIndex = -1;
}
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >
<ListBox x:Name="BookDetails">
<TextBlock x:Name="booktitle" Text="Book Title:" Foreground="#FF3B12AA" FontSize="30" FontWeight="Bold" FontFamily="Courier New"/>
<TextBox x:Name="booktitletext" Text="{Binding BookTitle, Mode=TwoWay}" Width="460" TextWrapping="Wrap" Background="#BF22A1DC" BorderBrush="#BFFFFFFF" FontFamily="Tahoma" FontSize="26"/>
<TextBlock x:Name="author" Text="Author:" Foreground="#FF3B12AA" FontSize="30" FontWeight="Bold" FontFamily="Courier New"/>
<TextBox x:Name="authortext" Tap="authortext_Tap" Text="{Binding Author, Mode=TwoWay}" Width="460" TextWrapping="Wrap" Background="#BF22A1DC" BorderBrush="#BFFFFFFF" FontFamily="Tahoma" FontSize="26"/>
<TextBlock x:Name="genre" Text="Genre:" Foreground="#FF3B12AA" FontSize="30" FontWeight="Bold" FontFamily="Courier New"/>
<toolkit:ListPicker x:Name="ListPicker" ItemTemplate="{StaticResource listpickertemplate}" Width="120" HorizontalAlignment="Left" Background="#BF3BDC22" SelectedIndex="{Binding Index}"/>
</ListBox>
</Grid>
public BookDetail()
{
InitializeComponent();
this.DataContext = App.MainViewModel;
List<GenrePicker> newpicker = new List<GenrePicker>();
newpicker.Add(new GenrePicker() { Genre = "Comedy",Index = 0});
newpicker.Add(new GenrePicker() { Genre = "Science", Index = 1 });
newpicker.Add(new GenrePicker() { Genre = "Action", Index = 2});
this.ListPicker.ItemsSource = newpicker;
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// Get the parent application
App thisApp = Application.Current as App;
// Load the active customer into the viewmodel
App.MainViewModel.LoadDetails(thisApp.SelectedBook,thisApp.SelectedGenre);
}
public void LoadDetails(Book bcd,GenrePicker xyz)
{
Index = xyz.Index;
BookTitle = bcd.BookTitle;
Genre = bcd.Genre;
}
public BookDetail()
{
InitializeComponent();
List<GenrePicker> newpicker = new List<GenrePicker>();
newpicker.Add(new GenrePicker() { Genre = "Comedy",Index = 0});
newpicker.Add(new GenrePicker() { Genre = "Science", Index = 1 });
newpicker.Add(new GenrePicker() { Genre = "Action", Index = 2});
this.ListPicker.ItemsSource = newpicker;
this.DataContext = App.MainViewModel;
}