首页 > 解决方案 > Xamarin CollectionView - 在 ItemTemplate 中访问父绑定?

问题描述

我有以下 XAML,其中包含 CollectionView 标头和关联的 CollectionView 项。数据源 (ItemsSource) 是一个名为“用户”的列表(如下所示)。

无论如何,我是否可以访问顶层的属性(即标题正在使用的属性)?

谢谢 !

<CollectionView Grid.Row="1" x:Name="users" IsGrouped="True" SelectionMode="Single">
            <CollectionView.GroupHeaderTemplate>
                <DataTemplate>
                    <StackLayout Orientation="Horizontal" MinimumHeightRequest="200" BackgroundColor="{Binding tint_colour}">
                        <Image Source="{Binding school_image}" WidthRequest="120" HeightRequest="100"/>
                        <Label Text="{Binding organisation_title}"                      
                                    TextColor="{Binding font_colour}"
                                    FontSize="Large"
                                    FontAttributes="Bold"
                                    VerticalTextAlignment="Center"></Label>
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer NumberOfTapsRequired="1" Tapped="HeaderTapped" CommandParameter="{Binding organisation_title}"></TapGestureRecognizer>
                        </StackLayout.GestureRecognizers>
                    </StackLayout>
                </DataTemplate>
            </CollectionView.GroupHeaderTemplate>
            <CollectionView.ItemsLayout>
                <LinearItemsLayout Orientation="Vertical" ItemSpacing="1"/>
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout HeightRequest="200">
                        <Grid IsVisible="true" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
                            <behaviors:Expander x:Name="MainExpander" CollapseAnimationLength="500" IsExpanded="false"  >
                                <behaviors:Expander.Header>
                                    <Grid HorizontalOptions="FillAndExpand">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="100"/>
                                            <ColumnDefinition Width="10"/>
                                        </Grid.ColumnDefinitions>
                                        <Frame HeightRequest="40" WidthRequest="40" CornerRadius="20" HorizontalOptions="Start" VerticalOptions="Center" Margin="20" Padding="0" BackgroundColor="Maroon">
                                            <Label Text="{Binding student_initial}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" />
                                        </Frame>
                                        <StackLayout Grid.Column="2" HorizontalOptions="StartAndExpand" VerticalOptions="Center" Margin="20">
                                            <Label x:Name="StudentName" Text="{Binding student_fullname}"></Label>
                                            <Label x:Name="StudentID" IsVisible="false" Text="{Binding school_image}"></Label>
                                        </StackLayout>
                                    </Grid>
                                </behaviors:Expander.Header>
                                <Grid RowSpacing="0" HorizontalOptions="FillAndExpand" HeightRequest="240" VerticalOptions="FillAndExpand">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <Button Grid.Row="0" Text="Messages" Clicked="Button_Clicked"></Button>
                                    <Button x:Name="btnTopUp" Grid.Row="1" Text="Quick Topup" Clicked="Button_Clicked" IsVisible="{Binding topup_product_id, Converter={StaticResource IsNotNullOrEmptyConverter}}"></Button>
                                    <Button Grid.Row="2" Text="Payments" Clicked="Button_Clicked"></Button>
                                </Grid>
                                <!--TODO: Look at adding a balance for childrens topups?-->
                            </behaviors:Expander>
                        </Grid>
                    </StackLayout>
                </DataTemplate>

            </CollectionView.ItemTemplate>
        </CollectionView>

这是我的用户 ItemsSource,它是 AccountGroup 项目的列表:

public class Account : INotifyPropertyChanged
    {
        public string student_unique_id { get; set; }

        public string student_fullname { get; set; }

        public string organisation_id { get; set; }

        public string organisation_title { get; set; }

        public string student_token { get;set;}

        public string reg_branding_url { get;set;}

        public string tint_colour { get;set;}

        public string font_colour { get;set;}

        public string topup_product_id { get;set;}

        private string _height;
        public string height
        {
            set { SetProperty(ref _height, value); }
            get { return _height; }
        }


        private bool _isVisible;
        public bool isVisible
        {
            set { SetProperty(ref _isVisible, value); }
            get { return _isVisible; }
        }

        public string student_initial 
        { 
            get
            {
                return student_fullname[0].ToString();
            } 
        }

        public string school_image
        {
            get
            {
                return $"{Constants.apiBaseUrl}store/images/uploaded/mobile/{reg_branding_url}";
            }
        }

        bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value))
                return false;

            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class AccountGroup : List<Account>, INotifyPropertyChanged
    {
        public string organisation_title { get; set; }
        public string school_image { get; set; }
        public string tint_colour { get; set; }
        public string font_colour { get; set; }

        public List<Account> accountList { get; set; }

        private bool _isVisible;
        public bool isVisible
        {
            set { SetProperty(ref _isVisible, value); }
            get { return _isVisible; }
        }

        bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value))
                return false;

            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        public AccountGroup(string orgTitle, string orgImage, string orgTint, string orgFont, List<Account> accounts) : base(accounts)
        {
            organisation_title = orgTitle;
            school_image = orgImage;
            tint_colour = orgTint;
            font_colour = orgFont;
            accountList = accounts;
        }



        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

标签: xamlxamarinxamarin.forms

解决方案


推荐阅读