-
Notifications
You must be signed in to change notification settings - Fork 83
Provide suitable DataRow behavior when DataTable is not present #784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using CommunityToolkit.WinUI.Controls; | ||
|
|
||
| namespace DataTableExperiment.Samples; | ||
|
|
||
| [ToolkitSample(id: nameof(DataRowWithoutDataTableSample), "DataRows without DataTable Example", description: $"A sample for showing the default layout of {nameof(DataRow)} withou {nameof(DataTable)} control.")] | ||
| public sealed partial class DataRowWithoutDataTableSample : Page | ||
| { | ||
| public const int NumberOfRows = 6; | ||
|
|
||
| public ObservableCollection<InventoryItem> InventoryItems { get; set; } | ||
|
|
||
| public DataRowWithoutDataTableSample() | ||
| { | ||
| InventoryItem[] items = new InventoryItem[NumberOfRows]; | ||
|
|
||
| for (int i = 0; i < NumberOfRows; i++) | ||
| { | ||
| items[i] = new() | ||
| { | ||
| Id = i, | ||
| Name = i.ToString(), | ||
| Description = i.ToString(), | ||
| Quantity = i, | ||
| }; | ||
| } | ||
|
|
||
| items[3].Name = "Hello, testing!"; | ||
|
|
||
| items[5].Description = "This is a very long description that should have been out of view at the start..."; | ||
|
|
||
| InventoryItems = new(items); | ||
|
|
||
| this.InitializeComponent(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information. --> | ||
| <Page x:Class="DataTableExperiment.Samples.DataRowWithoutDataTableSample" | ||
| xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
| xmlns:behaviors="using:CommunityToolkit.WinUI.Behaviors" | ||
| xmlns:controls="using:CommunityToolkit.WinUI.Controls" | ||
| xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
| xmlns:interactivity="using:Microsoft.Xaml.Interactivity" | ||
| xmlns:local="using:DataTableExperiment.Samples" | ||
| xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
| mc:Ignorable="d"> | ||
|
|
||
| <ListView ItemsSource="{x:Bind InventoryItems}"> | ||
|
|
||
| <!-- no DataTable in <ListView.Header> --> | ||
|
|
||
| <ListView.ItemTemplate> | ||
| <DataTemplate x:DataType="local:InventoryItem"> | ||
| <controls:DataRow> | ||
| <TextBlock VerticalAlignment="Center" | ||
| Text="{x:Bind Id}" /> | ||
| <TextBlock VerticalAlignment="Center" | ||
| Text="{x:Bind Name}" /> | ||
| <TextBlock VerticalAlignment="Center" | ||
| Text="{x:Bind Description}" /> | ||
| <TextBlock VerticalAlignment="Center" | ||
| Text="{x:Bind Quantity}" /> | ||
| </controls:DataRow> | ||
| </DataTemplate> | ||
| </ListView.ItemTemplate> | ||
| <!--<ListView.ItemContainerStyle> | ||
| <Style BasedOn="{StaticResource DefaultListViewItemStyle}" | ||
| TargetType="ListViewItem"> | ||
| <Setter Property="HorizontalContentAlignment" Value="Left" /> | ||
| </Style> | ||
| </ListView.ItemContainerStyle>--> | ||
|
Comment on lines
+31
to
+36
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unneeded? |
||
| </ListView> | ||
| </Page> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,17 +84,33 @@ protected override Size MeasureOverride(Size availableSize) | |
|
|
||
| double maxHeight = 0; | ||
|
|
||
| if (Children.Count > 0) | ||
| // If we don't have a grid, provides equal widths to child elements. | ||
| if (_parentPanel is null) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For both the measure and arrange bit we should probably abstract these to sub-methods to just make it a bit easier to contain. |
||
| { | ||
| // If we don't have a grid, just measure first child to get row height and take available space | ||
| if (_parentPanel is null) | ||
| double width = this.DesiredSize.Width; | ||
|
|
||
| if (!double.IsInfinity(availableSize.Width)) | ||
| width = Math.Max(width, availableSize.Width); | ||
|
|
||
| for (int i = 0; i < Children.Count; i++) | ||
| { | ||
| Children[0].Measure(availableSize); | ||
| return new Size(availableSize.Width, Children[0].DesiredSize.Height); | ||
| var child = Children[i]; | ||
| if (child?.Visibility != Visibility.Visible) | ||
| continue; | ||
|
|
||
| child.Measure(availableSize); | ||
|
|
||
| maxHeight = Math.Max(maxHeight, child.DesiredSize.Height); | ||
| } | ||
|
|
||
| return new Size(width, maxHeight); | ||
| } | ||
|
|
||
| if (Children.Count > 0) | ||
| { | ||
| // Handle DataTable Parent | ||
| else if (_parentTable != null | ||
| && _parentTable.Children.Count == Children.Count) | ||
| if (_parentTable != null && | ||
| _parentTable.Children.Count == Children.Count) | ||
| { | ||
| // TODO: Need to check visibility | ||
| // Measure all children since we need to determine the row's height at minimum | ||
|
|
@@ -178,19 +194,43 @@ protected override Size MeasureOverride(Size availableSize) | |
| /// <inheritdoc/> | ||
| protected override Size ArrangeOverride(Size finalSize) | ||
| { | ||
| // If we don't have a grid, provides equal widths to child elements. | ||
| if (_parentPanel is null) | ||
| { | ||
| if (Children.Count == 0) | ||
| return new Size(0, finalSize.Height); | ||
|
|
||
| double x = 0; | ||
| double width = finalSize.Width / Children.Count; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need the visible child count here otherwise we'll make everything smaller and not take up the full width, we can cache this from the measure step. |
||
|
|
||
| for (int i = 0; i < Children.Count; i++) | ||
| { | ||
| var child = Children[i]; | ||
| if (child?.Visibility != Visibility.Visible) | ||
| continue; | ||
|
|
||
| child.Arrange(new Rect(x, 0, width, finalSize.Height)); | ||
|
|
||
| x += width; | ||
| } | ||
|
|
||
| return new Size(x, finalSize.Height); | ||
| } | ||
|
|
||
| int column = 0; | ||
| double x = 0; | ||
|
|
||
| // Try and grab Column Spacing from DataTable, if not a parent Grid, if not 0. | ||
| double spacing = _parentTable?.ColumnSpacing ?? (_parentPanel as Grid)?.ColumnSpacing ?? 0; | ||
|
|
||
| double width = 0; | ||
|
|
||
| if (_parentPanel != null) | ||
| { | ||
| double x = 0; | ||
|
|
||
| int i = 0; | ||
| foreach (UIElement child in Children.Where(static e => e.Visibility == Visibility.Visible)) | ||
| { | ||
| double width = 0; | ||
|
|
||
| if (_parentPanel is Grid grid && | ||
| column < grid.ColumnDefinitions.Count) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sample is nice here thanks, this does seem like a useful base scenario, though someone could just use an EqualPanel here just as easily, it does extend itself later to just adding the rest of the pattern if needed.