У меня есть коллекция объектов внутри Grid. Каждый объект отображается внутри границы. Мне нужно, чтобы каждая плитка Border занимала все доступное горизонтальное пространство. Прямо сейчас у меня есть набор прямоугольников разных форм и размеров, который выглядит не очень хорошо.
Это определение контента:
var collectionView = new CollectionView();
collectionView.ItemTemplate = new DataTemplate(() =>
{
var border = new Border
{
Stroke = Colors.LightGray,
StrokeThickness = 1,
StrokeShape = new RoundRectangle { CornerRadius = 10 },
BackgroundColor = Colors.White//,
//HorizontalOptions = LayoutOptions.FillAndExpand //deprecated, doesn't do anything
};
var grid = new Grid
{
Padding = 10,
RowDefinitions = new RowDefinitionCollection
{
new RowDefinition(GridLength.Auto),
new RowDefinition(GridLength.Auto)
},
ColumnDefinitions = new ColumnDefinitionCollection
{
new ColumnDefinition(GridLength.Auto),
new ColumnDefinition(GridLength.Auto)
}
};
var titleLabel = new Label
{
FontAttributes = FontAttributes.Bold,
FontSize = 18,
TextColor = Colors.Black
};
titleLabel.SetBinding(Label.TextProperty, "Title");
var descriptionLabel = new Label
{
FontSize = 14,
TextColor = Colors.Gray
};
descriptionLabel.SetBinding(Label.TextProperty, "Description");
var buttonDelete = new Button
{
Text = "Delete",
BackgroundColor = Colors.Red,
TextColor = Colors.White,
Padding = 5,
FontSize = 12,
};
buttonDelete.Clicked += async (sender, e) =>
{
if (sender is Button btn && btn.BindingContext is Notification notification)
{
await _viewModel.Delete(notification.Id);
await _viewModel.Initialize();
}
};
buttonDelete.SetBinding(BindableObject.BindingContextProperty, ".");
grid.Add(titleLabel, 0, 0);
grid.Add(descriptionLabel, 0, 1);
grid.Add(buttonDelete, 1, 0);
border.Content = grid;
return border;
});
collectionView.ItemsLayout = new GridItemsLayout(1, ItemsLayoutOrientation.Vertical)
{
Span = 1 //doesn't do anything
};
У меня закончились идеи. Все либо ничего не делает, либо вызывает ошибки/предупреждения. LayoutOptions.Fill тоже ничего не делает.
Если никто здесь не знает, как это исправить, мне придется переключиться на разметку XAML, в разметке C# отсутствует необходимая документация.
У меня есть коллекция объектов внутри Grid. Каждый объект отображается внутри границы. Мне нужно, чтобы каждая плитка Border занимала все доступное горизонтальное пространство. Прямо сейчас у меня есть набор прямоугольников разных форм и размеров, который выглядит не очень хорошо. Это определение контента: [code]Content = new AbsoluteLayout { Children = { new ScrollView { Content = new Grid { Children = { collectionView .Bind(ItemsView.ItemsSourceProperty, nameof(_viewModel.Notifications)) } } }, buttonCorner.Text("New") .LayoutBounds(1, 1, -1, -1).LayoutFlags(AbsoluteLayoutFlags.PositionProportional) } }.BackgroundColor(Colors.Snow).Margin(20); [/code] Это определение CollectionView [code]var collectionView = new CollectionView(); collectionView.ItemTemplate = new DataTemplate(() => { var border = new Border { Stroke = Colors.LightGray, StrokeThickness = 1, StrokeShape = new RoundRectangle { CornerRadius = 10 }, BackgroundColor = Colors.White//, //HorizontalOptions = LayoutOptions.FillAndExpand //deprecated, doesn't do anything }; var grid = new Grid { Padding = 10, RowDefinitions = new RowDefinitionCollection { new RowDefinition(GridLength.Auto), new RowDefinition(GridLength.Auto) }, ColumnDefinitions = new ColumnDefinitionCollection { new ColumnDefinition(GridLength.Auto), new ColumnDefinition(GridLength.Auto) } };
var titleLabel = new Label { FontAttributes = FontAttributes.Bold, FontSize = 18, TextColor = Colors.Black }; titleLabel.SetBinding(Label.TextProperty, "Title"); var descriptionLabel = new Label { FontSize = 14, TextColor = Colors.Gray }; descriptionLabel.SetBinding(Label.TextProperty, "Description"); var buttonDelete = new Button { Text = "Delete", BackgroundColor = Colors.Red, TextColor = Colors.White, Padding = 5, FontSize = 12, }; buttonDelete.Clicked += async (sender, e) => { if (sender is Button btn && btn.BindingContext is Notification notification) { await _viewModel.Delete(notification.Id); await _viewModel.Initialize(); } }; buttonDelete.SetBinding(BindableObject.BindingContextProperty, ".");
return border; }); collectionView.ItemsLayout = new GridItemsLayout(1, ItemsLayoutOrientation.Vertical) { Span = 1 //doesn't do anything }; [/code] У меня закончились идеи. Все либо ничего не делает, либо вызывает ошибки/предупреждения. LayoutOptions.Fill тоже ничего не делает. Если никто здесь не знает, как это исправить, мне придется переключиться на разметку XAML, в разметке C# отсутствует необходимая документация.