问题
Currently i have a TasksGroupPageViewModel.cs that contains my two ObservableCollection:
public ObservableCollection<TasksGroup> TasksGroups { get; set; } = new ObservableCollection<TasksGroup>();
public ObservableCollection<Tasks> Taches1 { get; set; } = new ObservableCollection<Tasks>();
The binding for my CollectionView which uses the TasksGroups data works, BUT i have a bindable stacklayout inside it but i am unable to bind it to his ObservableCollection data (taches1)
From the debuger i can see that it always try to access data from Models.TasksGroup.cs and not Models.Tasks.cs, it's like since my stacklayout is a children of the CollectionView, i can't bind to it
My question is : How can i directly bind to Taches1 ObservableCollection which is in TaskGroupPageViewModel with BindableLayout.ItemsSource="" or in .xaml.cs file but i didn't find a way to do it like i did for TasksGroup in the on appearing method
How i initialize my data in TaskGroupPage.xaml.cs (data1 is for TasksGroup and data2 is for 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);
}
How i bind to my collection view with x:Name=TasksGroupCollection
<CollectionView Grid.Row="2" Margin="25" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
SelectionMode="Single" x:Name="TasksGroupCollection" >
Inside this CollectionView i have a bindable stack layout which i want to bind to Taches1 data
<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>
回答1:
From the debuger i can see that it always try to access data from Models.TasksGroup.cs and not Models.Tasks.cs
When any element is included in a DataTemplate
, the BindingContext
will be the current item from the template's ItemsSource.
Your TasksGroupCollection.ItemsSource
is vm.TasksGroups
and there is no property called Taches1
in vm.TasksGroups
, that's why you can't bind successfully.
Solution:
Taches1
is a property of vm
and you should set the correct binding Source:
First, give a name to your ContentPage, let's say it MyPage:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Name="MyPage"
x:Class="App266.MainPage">
Then the bindingContext
of your page is vm
, bind the BindableLayout.ItemsSource
to vm.Taches1
:
<StackLayout BindableLayout.ItemsSource="{Binding BindingContext.Taches1 , Source={x:Reference MyPage}}" HorizontalOptions="Start" VerticalOptions="Center" Margin="20,0,0,20" x:Name="Taches1">
来源:https://stackoverflow.com/questions/62700725/how-to-set-the-bindablelayout-itemssource-to-an-observablecollection-in-an-other