如何将 BindableLayout.ItemsSource 设置为其他文件中的 ObservableCollection [英] How to set the BindableLayout.ItemsSource to an ObservableCollection in an other file
问题描述
目前我有一个 TasksGroupPageViewModel.cs,其中包含我的两个 ObservableCollection:
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>();
我的 CollectionView 使用 TasksGroups 数据的绑定有效,但我在里面有一个可绑定的堆栈布局,但我无法将它绑定到他的 ObservableCollection 数据 (taches1)
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)
从调试器我可以看到它总是尝试从 Models.TasksGroup.cs 而不是 Models.Tasks.cs 访问数据,就像因为我的 stacklayout 是 CollectionView 的子项,我无法绑定给它
我的问题是:如何使用 BindableLayout.ItemsSource=""直接绑定到 TaskGroupPageViewModel 中的 Taches1 ObservableCollection?或在 .xaml.cs 文件中,但我没有找到像我在出现的方法中为 TasksGroup 所做的那样的方法
我如何在 TaskGroupPage.xaml.cs 中初始化我的数据(data1 用于 TasksGroup,data2 用于任务:
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>
推荐答案
从调试器我可以看到它总是尝试从Models.TasksGroup.cs 而不是 Models.Tasks.cs
From the debuger i can see that it always try to access data from Models.TasksGroup.cs and not Models.Tasks.cs
当任何元素包含在 DataTemplate
中时,BindingContext
将是模板的 ItemsSource 中的当前项目.
When any element is included in a DataTemplate
, the BindingContext
will be the current item from the template's ItemsSource.
您的 TasksGroupCollection.ItemsSource
是 vm.TasksGroups
并且 vm.TasksGroups
中没有名为 Taches1
的属性,这就是你无法绑定成功的原因.
Your TasksGroupCollection.ItemsSource
is vm.TasksGroups
and there is no property called Taches1
in vm.TasksGroups
, that's why you can't bind successfully.
解决方案:
Taches1
是 vm
的一个属性,你应该设置正确的绑定源:
Taches1
is a property of vm
and you should set the correct binding Source:
首先,为您的 ContentPage 命名,例如 MyPage:
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">
那么你页面的bindingContext
就是vm
,把BindableLayout.ItemsSource
绑定到vm.Taches1
:
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">
这篇关于如何将 BindableLayout.ItemsSource 设置为其他文件中的 ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!