xamarin forms bindable属性不能正常工作。

问题描述 投票:0回答:1

我试图创建像下面这样的自定义视图,我试图将主页面的一个集合绑定到自定义视图中。

但在这里却没有正确绑定。

<ContentView.Content>
        <Grid
            x:Name="Layout"
            HorizontalOptions="FillAndExpand"
            VerticalOptions="FillAndExpand">

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <StackLayout
                Grid.Row="0"
                BackgroundColor="#E0E0E0"
                Padding="20,10">
                <Label Text="{Binding Source={x:Reference ExpandableContentView}, Path=Header}"/>
            </StackLayout>

            <StackLayout
                Grid.Row="1"
                BindableLayout.ItemsSource="{Binding Source={x:Reference ExpandableContentView}, Path=SummeryList}">

                <Label Text="{Binding Test}"/>
            </StackLayout>

        </Grid>
    </ContentView.Content>

xmal.cs文件

public partial class ExpandableView : ContentView
    {
        public static readonly BindableProperty HeaderProperty = BindableProperty.Create(
            nameof(Header),
            typeof(string),
            typeof(ExpandableView),
            default(string));

        public static readonly BindableProperty SummeryListProperty = BindableProperty.Create(
            nameof(SummeryList),
            typeof(ObservableCollection<SummeryDetailModel>),
            typeof(ExpandableView),
            propertyChanged: CollectionChanged);

        private static void CollectionChanged(BindableObject bindable, object oldValue, object newValue)
        {
        }

        public ExpandableView()
        {
            InitializeComponent();
        }

        public string Header
        {
            get => (string)GetValue(HeaderProperty);
            set => SetValue(HeaderProperty, value);
        }

        public ObservableCollection<SummeryDetailModel> SummeryList
        {
            get => (ObservableCollection<SummeryDetailModel>)GetValue(SummeryListProperty);
            set => SetValue(SummeryListProperty, value);
        }

我在MainPage中做了如下绑定。

<commonviews:ExpandableView
            Header="Shipment Details"
            SummeryList="{Binding SummeryCollection}">
        </commonviews:ExpandableView>

我的 SummeryDetailModel 喜欢

public class SummeryDetailModel
    {
        public string Test { get; set; }
    }

在这里,它并不约束 测试 价值的标签。

我有什么遗漏的地方吗?

c# xamarin.forms bindableproperty
1个回答
0
投票

我刚刚发现,我做了一个小错误。

我没有添加 项目模板 数据模板 对于 栈式布局 BindableLayout

具体如下

<StackLayout
                Grid.Row="1"
                Padding="20,10"
                Spacing="15"
                BindableLayout.ItemsSource="{Binding Source={x:Reference ExpandableContentView}, Path=SummeryList}">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <StackLayout
                            Spacing="10">
                            <Label
                                Text="{Binding Title}"
                                FontFamily="{StaticResource LatoRegular}"
                                FontSize="{StaticResource FontSize14}"
                                TextColor="{StaticResource PlaceholderColor}"/>

                            <Label Text="{Binding Detail}"
                                   FontFamily="{StaticResource LatoRegular}"
                                   FontSize="{StaticResource FontSize14}"
                                   TextColor="{StaticResource PrimaryText}"/>

                        </StackLayout>
                    </DataTemplate>
                </BindableLayout.ItemTemplate>
            </StackLayout>
© www.soinside.com 2019 - 2024. All rights reserved.