繁体   English   中英

Listview SelectedItem不绑定

[英]Listview SelectedItem not binding

我遇到一个问题,尝试使用Listview显示对象列表并将其发送到新页面。 但是,BrandBox.isSelected返回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));
        }

尝试这样做

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM