【问题标题】:How to set the BindableLayout.ItemsSource to an ObservableCollection in an other file如何将 BindableLayout.ItemsSource 设置为其他文件中的 ObservableCollection
【发布时间】:2020-07-02 16:13:15
【问题描述】:

目前我有一个包含我的两个 ObservableCollection 的 TasksGroupPageViewModel.cs:

public ObservableCollection<TasksGroup> TasksGroups { get; set; } = new ObservableCollection<TasksGroup>();
public ObservableCollection<Tasks> Taches1 { get; set; } = new ObservableCollection<Tasks>();

我的 CollectionView 使用 TasksGroups 数据的绑定有效,但是我里面有一个可绑定的堆栈布局,但我无法将它绑定到他的 ObservableCollection 数据 (taches1)

从调试器中我可以看到它总是尝试从 Models.TasksGroup.cs 而不是 Models.Tasks.cs 访问数据,这就像因为我的 stacklayout 是 CollectionView 的子项,我无法绑定给它

我的问题是:我怎样才能直接绑定到 Taches1 ObservableCollection,它位于 TaskGroupPageViewModel 中,带有 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>

【问题讨论】:

  • 我的解决方案对您有用吗?如果是,请您接受(点击此答案左上角的☑️),以便我们可以帮助更多有相同问题的人:)。

标签: c# xamarin xamarin.forms


【解决方案1】:

从调试器我可以看到它总是尝试从 Models.TasksGroup.cs 而不是 Models.Tasks.cs

DataTemplate 中包含任何元素时,BindingContext 将是模板的 ItemsSource 中的当前项。

你的TasksGroupCollection.ItemsSourcevm.TasksGroups,而vm.TasksGroups中没有名为Taches1的属性,所以无法绑定成功。

解决方案:

Taches1vm 的属性,您应该设置正确的绑定来源:

首先,给你的 ContentPage 起个名字,比如说 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

<StackLayout BindableLayout.ItemsSource="{Binding BindingContext.Taches1 , Source={x:Reference MyPage}}"  HorizontalOptions="Start" VerticalOptions="Center" Margin="20,0,0,20" x:Name="Taches1">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 2017-12-01
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多