I'm having an issue where my attempting to use a listview to display a list of objects, and send them to a new page. However, BrandBox.isSelected is returning null.
.xaml
<Grid >
<ListView x:Name="BrandBox" HorizontalAlignment="Stretch" VerticalAlignment="Top" SelectedItem="{Binding ItemSelected, Mode=TwoWay}" SelectionChanged="Selector_OnSelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate >
<DataTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="128">
<Image Source="{Binding BrandImageLoc}" HorizontalAlignment="Stretch" VerticalAlignment="Top" Stretch="UniformToFill" />
<TextBlock Text="{Binding BrandName}" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
.cs
List<Brand> Brands = new List<Brand>();
public ManufactuerList()
{
InitializeComponent();
Brands = App.career.Brands;
this.BrandBox.ItemsSource = Brands;
}
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Brand selectedBrand = BrandBox.SelectedItem as Brand;
this.NavigationService.Navigate(new CarsPurchaseable(selectedBrand));
}
Try doing like this
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListView brandBox = sender as ListView;
if(brandBox != null)
{
Brand selectedBrand = brandBox.SelectedItem as Brand;
if(selectedBrand != null)
{
this.NavigationService.Navigate(new CarsPurchaseable(selectedBrand));
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.