我正在尝试构建一个名为WrapLayout
的自定义布局,它在水平而不是垂直方向上打印ObservableCollection中类型为ListItem
的对象。我想我已经掌握了基础知识,但是我无法自己设置ObservableCollection。
我创建了一个名为GetListItems
的方法,它返回所需的ListItems ObservableCollection。以前,使用名为inactiveList
的普通ListView,我设置了绑定数据,如下所示:
ObservableCollection<ListItem> inactiveItems =
new ObservableCollection<ListItem>(
App.ListItemRepo.GetListItems());
inactiveList.ItemsSource = inactiveItems;
但我无法弄清楚如何将ObservableCollection inactiveItems
添加到我当前的代码中。 如何更改当前代码以指定我想在自定义WrapLayout中显示的ListItem?
的Xaml:
<ContentPage xmlns:local="clr-namespace:Myapp">
<local:WrapLayout x:Name="inactiveList" ItemsSource="{Binding .}" Spacing="5" />
代码隐藏:
public class WrapLayout : Layout<View>
{
public ObservableCollection<ListItem> ItemsSource
{
get { return (ObservableCollection<ListItem>)GetValue(ItemSourceProperty); }
set { SetValue(ItemSourceProperty, value); }
}
public static readonly BindableProperty ItemSourceProperty =
BindableProperty.Create
(
"ItemsSource",
typeof(ObservableCollection<ListItem>),
typeof(WrapLayout),
propertyChanged: (bindable, oldvalue, newvalue) => ((WrapLayout)bindable).AddViews()
);
void AddViews()
{
Children.Clear();
foreach (ListItem s in ItemsSource)
{
Label label = new Label();
label.BackgroundColor = Color.Red;
label.Text = s.Name;
label.TextColor = Color.Black;
Children.Add(label);
}
}
public static readonly BindableProperty SpacingProperty =
BindableProperty.Create
(
"Spacing",
typeof(double),
typeof(WrapLayout),
10.0,
propertyChanged: (bindable, oldvalue, newvalue) => ((WrapLayout)bindable).OnSizeChanged()
);
<!-- methods for setting styling -->
}