[英]Listview SelectedItem not binding
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. 我遇到一个问题,尝试使用Listview显示对象列表并将其发送到新页面。 However, BrandBox.isSelected is returning null. 但是,BrandBox.isSelected返回null。
.xaml .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 .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));
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.