XAML .NET MAUI选取器项源绑定不工作

44u64gxh  于 2023-08-01  发布在  .NET
关注(0)|答案(1)|浏览(174)

对这个简单的绑定有问题。
尝试将字符串名称“carNames”的列表绑定到选择器,但不起作用。
当我将Picker.ItemSource设置为我的List“CarNames”时,我得到了显示的项目,但是当我在XAML中绑定时,它不起作用。
我试着把它变成一个ObservableCollection,但这对我也不起作用。
代码隐藏

  1. using People.ViewModel;
  2. namespace People.View;
  3. public partial class CarView : ContentPage
  4. {
  5. List<Cars> cars;
  6. CarSet carSet;
  7. public List<string> carNames { get; set; } = new();
  8. public CarView()
  9. {
  10. carSet = new CarSet(carNames);
  11. InitializeComponent();
  12. }
  13. public void OnNewButtonCarClicked(object sender, EventArgs args)
  14. {
  15. CarMessage.Text = "";
  16. App.CarRepo.AddNewCar(newModel.Text);
  17. CarMessage.Text = App.CarRepo.CarMessage;
  18. }
  19. public void OnGetButtonCarClicked(object sender, EventArgs args)
  20. {
  21. CarMessage.Text = "";
  22. cars = App.CarRepo.GetAllCars();
  23. carsList.ItemsSource = cars;
  24. }
  25. }

字符串
XAML

  1. <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  3. x:Class="People.View.CarView"
  4. Title="CarView">
  5. <Grid RowDefinitions="80,80,80,80,80,*"
  6. ColumnDefinitions="1000,*,*,*"
  7. BackgroundColor="Black">
  8. <Entry x:Name="newModel"
  9. Placeholder="Enter Vehicle"
  10. Grid.Column="1"
  11. Grid.ColumnSpan="2"/>
  12. <Button Text="Add Car"
  13. Grid.Row="2"
  14. Grid.Column="1"
  15. Grid.ColumnSpan="2"
  16. Clicked="OnNewButtonCarClicked" />
  17. <CollectionView x:Name="CarList"></CollectionView>
  18. <Label x:Name="CarMessage"
  19. Grid.Row="3"
  20. Grid.Column="1"/>
  21. <Button Text="Get All Cars"
  22. Grid.Row="4"
  23. Grid.Column ="1"
  24. Grid.ColumnSpan="2"
  25. Clicked="OnGetButtonCarClicked" />
  26. <Picker x:Name="Picker"
  27. Title="Select a Car"
  28. ItemsSource="{Binding CarNames}"
  29. ItemDisplayBinding="{Binding Cars}"
  30. Grid.Column="0"/>
  31. <CollectionView x:Name="carsList"
  32. Grid.Row="5"
  33. Grid.Column ="1"
  34. Grid.ColumnSpan="2"
  35. SelectionMode="Single">
  36. <CollectionView.ItemTemplate>
  37. <DataTemplate>
  38. <Grid>
  39. <Grid.ColumnDefinitions>
  40. <ColumnDefinition Width="*" />
  41. <ColumnDefinition Width="*" />
  42. <ColumnDefinition Width="*"/>
  43. </Grid.ColumnDefinitions>
  44. <Label Text="{Binding CarId}"
  45. Grid.Column="0"
  46. FontSize="Medium"/>
  47. <Label Grid.Column="2"
  48. Text="{Binding Car}"
  49. FontSize="Medium"/>
  50. </Grid>
  51. </DataTemplate>
  52. </CollectionView.ItemTemplate>
  53. </CollectionView>
  54. </Grid>
  55. </ContentPage>```

zkure5ic

zkure5ic1#

我在这段代码中发现了几个问题。请按照以下步骤操作。
1.首先需要设置绑定上下文。在构造函数中添加以下代码
String = this;
1.在Xaml Picker中,“ItemsSource”和“ItemDisplayBinding”绑定值不正确。像下面这样更改该部分。

  1. <Picker x:Name="Picker"
  2. Title="Select a Car"
  3. ItemsSource="{Binding carNames}"
  4. ItemDisplayBinding="{Binding cars}"
  5. Grid.Column="0"/>

字符串
ItemsSource="{BindingcarNames}”
ItemDisplayBinding="{Binding汽车}”

相关问题