目前我有一个 TasksGroupPageViewModel.cs,其中包含我的两个 ObservableCollection:
public ObservableCollection<TasksGroup> TasksGroups { get; set; } = new ObservableCollection<TasksGroup>();
public ObservableCollection<Tasks> Taches1 { get; set; } = new ObservableCollection<Tasks>();
使用 TasksGroups 数据的 CollectionView 的绑定有效,但我内部有一个可绑定的堆栈布局,但我无法将其绑定到他的 ObservableCollection 数据(taches1)
从调试器中我可以看到它总是尝试从 Models.TaskGroup.cs 而不是 Models.Tasks.cs 访问数据,就像因为我的 stacklayout 是 CollectionView 的子级,所以我无法绑定到它
我的问题是:我如何直接绑定到 TaskGroupPageViewModel 中的 Taches1 ObservableCollection 和 BindableLayout.ItemsSource="" 或 .xaml.cs 文件,但我没有找到一种方法来做到这一点,就像我在出现的 TasksGroup 中所做的那样方法
我如何在 TaskGroupPage.xaml.cs 中初始化数据(data1 用于 TasksGroup,data2 用于 Tasks :
protected override async void OnAppearing()
{
base.OnAppearing();
var data2 = await App.Database.GetAllTasks();
var data1 = await App.Database.GetTaskGroupsAsync();
var vm = this.BindingContext as TasksGroupPageViewModel;
vm.TasksGroups = new ObservableCollection<TasksGroup>(data1);
TasksGroupCollection.ItemsSource = vm.TasksGroups;
vm.Taches1 = new ObservableCollection<Tasks>(data2);
}
我如何使用 x:Name=TasksGroupCollection 绑定到我的集合视图
<CollectionView Grid.Row="2" Margin="25" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
SelectionMode="Single" x:Name="TasksGroupCollection" >
在这个 CollectionView 中,我有一个可绑定的堆栈布局,我想将其绑定到 Taches1 数据
<StackLayout Grid.Column="2" Spacing="10" >
<Label Text="Tâches" TextColor="Black" FontSize="15" Margin="20,0"/>
<StackLayout BindableLayout.ItemsSource="{Binding }" HorizontalOptions="Start" VerticalOptions="Center" Margin="20,0,0,20" x:Name="Taches1">
<BindableLayout.ItemTemplate >
<DataTemplate>
<Label TextColor="#2F3246" FontSize="12">
<Label.FormattedText>
<FormattedString>
<FormattedString.Spans>
<Span Text="{Binding TaskDBA}"/>
<Span Text=" - "/>
<Span Text="{Binding TaskDescription}" FontAttributes="Bold"/>
</FormattedString.Spans>
</FormattedString>
</Label.FormattedText>
</Label>
</DataTemplate>
</BindableLayout.ItemTemplate >
</StackLayout>
</StackLayout>