簡體   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