Skip to content

Commit c80cb49

Browse files
Magnus TorsgårdenMagnus Torsgården
authored andcommitted
Merge from main
2 parents 3525d77 + 254ed49 commit c80cb49

15 files changed

Lines changed: 644 additions & 175 deletions

samples/WinUI.TableView.SampleApp/MainPage.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ private void OnNavigationSelectionChanged(NavigationView sender, NavigationViewS
115115
"Data Export" => typeof(ExportPage),
116116
"Large Dataset" => typeof(LargeDataPage),
117117
"Conditional Cell Styling" => typeof(ConditionalStylingPage),
118+
"Column Sizing" => typeof(ColumnSizingPage),
118119
_ => typeof(BlankPage)
119120
};
120121

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<Page x:Class="WinUI.TableView.SampleApp.Pages.ColumnSizingPage"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:controls="using:WinUI.TableView.SampleApp.Controls"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:local="using:WinUI.TableView.SampleApp"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
xmlns:tv="using:WinUI.TableView"
9+
xmlns:ui="using:CommunityToolkit.WinUI"
10+
d:DataContext="{d:DesignInstance Type=local:ExampleViewModel}"
11+
mc:Ignorable="d"
12+
DataContextChanged="OnDataContextChanged">
13+
14+
<Grid>
15+
<controls:SamplePresenter Description="This sample demonstrates the different ways a column can be sized."
16+
Header="Column Sizing">
17+
<controls:SamplePresenter.Example>
18+
<tv:TableView x:Name="tableView"
19+
ItemsSource="{Binding Items}" />
20+
</controls:SamplePresenter.Example>
21+
<controls:SamplePresenter.Options>
22+
<StackPanel Spacing="16"
23+
MinWidth="200">
24+
<ComboBox x:Name="columnAutoWidthMode"
25+
HorizontalAlignment="Stretch"
26+
Header="Column Auto Width Mode"
27+
SelectedItem="{Binding ColumnAutoWidthMode, Mode=TwoWay, ElementName=tableView}"
28+
ItemsSource="{ui:EnumValues Type=tv:TableViewColumnAutoWidthMode}" />
29+
</StackPanel>
30+
</controls:SamplePresenter.Options>
31+
<controls:SamplePresenter.Xaml>
32+
<x:String xml:space="preserve">
33+
&lt;tv:TableView ItemsSource="{Binding Items}"
34+
ColumnAutoWidthMode="$(ColumnAutoWidthMode)"/>
35+
</x:String>
36+
</controls:SamplePresenter.Xaml>
37+
<controls:SamplePresenter.Substitutions>
38+
<controls:CodeSubstitution Key="ColumnAutoWidthMode"
39+
Value="{x:Bind columnAutoWidthMode.SelectedItem, Mode=OneWay}" />
40+
</controls:SamplePresenter.Substitutions>
41+
</controls:SamplePresenter>
42+
</Grid>
43+
</Page>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.UI.Xaml;
2+
using Microsoft.UI.Xaml.Controls;
3+
4+
namespace WinUI.TableView.SampleApp.Pages;
5+
6+
public sealed partial class ColumnSizingPage : Page
7+
{
8+
public ColumnSizingPage()
9+
{
10+
InitializeComponent();
11+
12+
}
13+
14+
private void OnDataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
15+
{
16+
if (DataContext is not ExampleViewModel viewModel) return;
17+
18+
viewModel.Items = [.. ExampleViewModel.ItemsList.Take(20)];
19+
}
20+
}

src/Columns/TableViewColumn.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ public double ActualWidth
237237
set => SetValue(ActualWidthProperty, value);
238238
}
239239

240+
/// <summary>
241+
/// Gets or sets the ColumnAutoWidthMode of the column.
242+
/// </summary>
243+
public TableViewColumnAutoWidthMode? ColumnAutoWidthMode
244+
{
245+
get => (TableViewColumnAutoWidthMode?)GetValue(ColumnAutoWidthModeProperty);
246+
set => SetValue(ColumnAutoWidthModeProperty, value);
247+
}
248+
240249
/// <summary>
241250
/// Gets or sets a value indicating whether the column can be resized.
242251
/// </summary>
@@ -506,6 +515,17 @@ private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChan
506515
}
507516
}
508517

518+
/// <summary>
519+
/// Handles changes to the ColumnAutoWidthMode property.
520+
/// </summary>
521+
private static void OnColumnAutoWidthModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
522+
{
523+
if (d is TableViewColumn column)
524+
{
525+
column.TableView?.RefreshColumnsAutoWidth([column]);
526+
}
527+
}
528+
509529
/// <summary>
510530
/// Handles changes to the IsReadOnly property.
511531
/// </summary>
@@ -598,6 +618,11 @@ public string? SortMemberPath
598618
/// </summary>
599619
public static readonly DependencyProperty ActualWidthProperty = DependencyProperty.Register(nameof(ActualWidth), typeof(double), typeof(TableViewColumn), new PropertyMetadata(0d, OnPropertyChanged));
600620

621+
/// <summary>
622+
/// Identifies the ColumnAutoWidthMode dependency property.
623+
/// </summary>
624+
public static readonly DependencyProperty ColumnAutoWidthModeProperty = DependencyProperty.Register(nameof(ColumnAutoWidthMode), typeof(TableViewColumnAutoWidthMode?), typeof(TableViewColumn), new PropertyMetadata(null, OnColumnAutoWidthModeChanged));
625+
601626
/// <summary>
602627
/// Identifies the CanResize dependency property.
603628
/// </summary>

0 commit comments

Comments
 (0)