Я пытаюсь составить список элементов, в котором каждый элемент имеет одинаковое количество столбцов, содержащих метку. Поскольку текст в каждой метке имеет разный размер, я хочу, чтобы столбцы составляли процент от доступной ширины, чтобы они выстраивались между различными элементами списка. Здесь я сталкиваюсь с проблемами. Несмотря на то, что я установил ширину столбца сетки на «*», они не используют доступную ширину. Я ищу решение, сравнимое с WPF SharedSizeGroup.
tldr: ListView в .net MAUI с равномерно расположенными столбцами по элементам списка.
<ListView ItemsSource = "{Binding TransactionViewmodels}" Grid.Row = "1">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid ColumnSpacing = "24" Margin = "24">
<Grid.RowDefinitions>
<RowDefinition Height = "Auto"/>
<RowDefinition Height = "Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width = "*"/>
<ColumnDefinition Width = "*"/>
<ColumnDefinition Width = "*"/>
<ColumnDefinition Width = "*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column = "0"
Grid.Row = "0"
FontSize = "Medium"
Text = "{Binding Date}"/>
<Label Grid.Column = "0"
Grid.Row = "1"
FontSize = "Small"
Text = "{Binding Time}"/>
<Label Grid.Column = "1"
Grid.Row = "0"
FontSize = "Medium"
Text = "{Binding StockName}"/>
<Label Grid.Column = "1"
Grid.Row = "1"
FontSize = "Small"
Text = "{Binding Isin}"/>
<Label Grid.Column = "2"
Grid.Row = "0"
Grid.RowSpan = "2"
FontSize = "Small"
VerticalTextAlignment = "Center"
Text = "{Binding Description}"/>
<Label Grid.Column = "3"
Grid.Row = "0"
Grid.RowSpan = "2"
FontSize = "Medium"
VerticalTextAlignment = "Center"
Text = "{Binding Value}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Изображение текущего результата
Обновлено: Xaml полной страницы.
<?xml version = "1.0" encoding = "utf-8" ?>
<ContentPage xmlns = "http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x = "http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local = "clr-namespace:PortfolioOverview.View.Desktop"
x:Class = "PortfolioOverview.View.Desktop.DesktopTransactionsPage"
Title = "DesktopTransactionsPage"
NavigationPage.HasNavigationBar = "False">
<HorizontalStackLayout>
<local:DesktopMenuView/>
<Grid ColumnDefinitions = "*" RowDefinitions = "auto, *">
<!--Add transaction UI goes here Grid.Row 0-->
<ListView ItemsSource = "{Binding TransactionViewmodels}" Grid.Row = "1">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid ColumnSpacing = "24" Margin = "24">
<Grid.RowDefinitions>
<RowDefinition Height = "Auto"/>
<RowDefinition Height = "Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width = "*"/>
<ColumnDefinition Width = "*"/>
<ColumnDefinition Width = "*"/>
<ColumnDefinition Width = "*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column = "0"
Grid.Row = "0"
FontSize = "Medium"
Text = "{Binding Date}"/>
<Label Grid.Column = "0"
Grid.Row = "1"
FontSize = "Small"
Text = "{Binding Time}"/>
<Label Grid.Column = "1"
Grid.Row = "0"
FontSize = "Medium"
Text = "{Binding StockName}"/>
<Label Grid.Column = "1"
Grid.Row = "1"
FontSize = "Small"
Text = "{Binding Isin}"/>
<Label Grid.Column = "2"
Grid.Row = "0"
Grid.RowSpan = "2"
FontSize = "Small"
VerticalTextAlignment = "Center"
Text = "{Binding Description}"/>
<Label Grid.Column = "3"
Grid.Row = "0"
Grid.RowSpan = "2"
FontSize = "Medium"
VerticalTextAlignment = "Center"
Text = "{Binding Value}"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</HorizontalStackLayout>
</ContentPage>
Мне удалось воспроизвести ошибку, если предположить, что у вас есть список внутри Grid с автоматическим определением столбца:
Не изменяя ваш xaml, я всего лишь присвоил определение столбца *, и оно выглядит так:
Я предполагаю, что ваша проблема каким-то образом связана с контейнером ListView, а не с itemTemplate.
Ах, смешно, наверное, из-за HorizontalStackLayout, он вам действительно нужен? если у вас есть только local:desktopmenuview и список, вы можете сделать что-то вроде <Grid ColumnDefinitions = "Auto, *"> вместо HorizontalStackLayout
Изменение HorizontalStackLayout на Grid решило проблему. Кажется странным, но это работает. Спасибо за вашу помощь!
Спасибо за ваш ответ, я обновил свой пост и добавил туда полный код xaml. Как вы можете видеть, у меня уже установлены определения *. Вы случайно не знаете, что может стать причиной этого?