Skip to content

Commit a5f615d

Browse files
feat(ControlCatalog): Add sample how binding DataGridColumn Width
1 parent ed6331c commit a5f615d

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Globalization;
3+
using Avalonia.Data.Converters;
4+
5+
namespace ControlCatalog.Models;
6+
7+
internal class GDPdLengthConverter : IValueConverter
8+
{
9+
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
10+
{
11+
if (value is double d)
12+
{
13+
return new Avalonia.Controls.DataGridLength(d,Avalonia.Controls.DataGridLengthUnitType.Pixel,d,d);
14+
}
15+
else if (value is decimal d2)
16+
{
17+
var dv =System.Convert.ToDouble(d2);
18+
return new Avalonia.Controls.DataGridLength(dv, Avalonia.Controls.DataGridLengthUnitType.Pixel, dv, dv);
19+
}
20+
return value;
21+
}
22+
23+
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
24+
{
25+
if (value is Avalonia.Controls.DataGridLength width)
26+
{
27+
return System.Convert.ToDecimal(width.DisplayValue);
28+
}
29+
return value;
30+
}
31+
}

samples/ControlCatalog/Pages/DataGridPage.xaml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<UserControl xmlns="https://github.com/avaloniaui"
22
xmlns:local="using:ControlCatalog.Models"
3+
xmlns:lc="using:ControlCatalog.Converter"
34
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
45
xmlns:pages="clr-namespace:ControlCatalog.Pages"
56
x:Class="ControlCatalog.Pages.DataGridPage"
67
x:DataType="pages:DataGridPage">
78
<UserControl.Resources>
9+
810
<local:GDPValueConverter x:Key="GDPConverter" />
11+
<local:GDPdLengthConverter x:Key="GDPWidthConverter"/>
912
<DataTemplate x:Key="Demo.DataTemplates.CountryHeader" x:DataType="local:Country">
1013
<StackPanel Orientation="Horizontal" Spacing="5">
1114
<PathIcon Height="12" Data="M 255 116 A 1 1 0 0 0 254 117 L 254 130 A 1 1 0 0 0 255 131 A 1 1 0 0 0 256 130 L 256 123.87109 C 256.1125 123.90694 256.2187 123.94195 256.33984 123.97852 C 257.18636 124.23404 258.19155 124.5 259 124.5 C 259.80845 124.5 260.52133 124.2168 261.17773 123.9668 C 261.83414 123.7168 262.43408 123.5 263 123.5 C 263.56592 123.5 264.5612 123.73404 265.37109 123.97852 C 266.18098 124.22299 266.82227 124.4668 266.82227 124.4668 A 0.50005 0.50005 0 0 0 267.5 124 L 267.5 118 A 0.50005 0.50005 0 0 0 267.17773 117.5332 C 267.17773 117.5332 266.50667 117.27701 265.66016 117.02148 C 264.81364 116.76596 263.80845 116.5 263 116.5 C 262.19155 116.5 261.47867 116.7832 260.82227 117.0332 C 260.16586 117.2832 259.56592 117.5 259 117.5 C 258.43408 117.5 257.4388 117.26596 256.62891 117.02148 C 256.39123 116.94974 256.17716 116.87994 255.98047 116.81445 A 1 1 0 0 0 255 116 z M 263 117.5 C 263.56592 117.5 264.5612 117.73404 265.37109 117.97852 C 266.00097 118.16865 266.29646 118.28239 266.5 118.35742 L 266.5 120.29297 C 266.25708 120.21012 265.97978 120.11797 265.66016 120.02148 C 264.81364 119.76596 263.80845 119.5 263 119.5 C 262.19155 119.5 261.47867 119.7832 260.82227 120.0332 C 260.16586 120.2832 259.56592 120.5 259 120.5 C 258.43408 120.5 257.4388 120.26596 256.62891 120.02148 C 256.39971 119.9523 256.19148 119.88388 256 119.82031 L 256 117.87109 C 256.1125 117.90694 256.2187 117.94195 256.33984 117.97852 C 257.18636 118.23404 258.19155 118.5 259 118.5 C 259.80845 118.5 260.52133 118.2168 261.17773 117.9668 C 261.83414 117.7168 262.43408 117.5 263 117.5 z M 263 120.5 C 263.56592 120.5 264.5612 120.73404 265.37109 120.97852 C 265.8714 121.12954 266.2398 121.25641 266.5 121.34961 L 266.5 123.30469 C 266.22286 123.20649 266.12863 123.1629 265.66016 123.02148 C 264.81364 122.76596 263.80845 122.5 263 122.5 C 262.19155 122.5 261.47867 122.7832 260.82227 123.0332 C 260.16586 123.2832 259.56592 123.5 259 123.5 C 258.43408 123.5 257.4388 123.26596 256.62891 123.02148 C 256.39971 122.9523 256.19148 122.88388 256 122.82031 L 256 120.87109 C 256.1125 120.90694 256.2187 120.94195 256.33984 120.97852 C 257.18636 121.23404 258.19155 121.5 259 121.5 C 259.80845 121.5 260.52133 121.2168 261.17773 120.9668 C 261.83414 120.7168 262.43408 120.5 263 120.5 z" />
@@ -28,8 +31,18 @@
2831
<TabControl Grid.Row="2">
2932
<TabItem Header="DataGrid">
3033
<DockPanel>
31-
<CheckBox x:Name="ShowGDP" IsChecked="True" Content="Toggle GDP Column Visibility"
32-
DockPanel.Dock="Top"/>
34+
<StackPanel Orientation="Horizontal"
35+
DockPanel.Dock="Top"
36+
Spacing="5">
37+
<CheckBox x:Name="ShowGDP" IsChecked="True" Content="Toggle GDP Column Visibility"/>
38+
<TextBlock Text="GDP Width:" VerticalAlignment="Center"/>
39+
<NumericUpDown x:Name="GDPWidth"
40+
Minimum="200"
41+
Maximum="350"
42+
Width="200"
43+
Increment="10"
44+
Value="200"/>
45+
</StackPanel>
3346
<DataGrid Name="dataGrid1" Margin="12" CanUserResizeColumns="True" CanUserReorderColumns="True" CanUserSortColumns="True" HeadersVisibility="All"
3447
RowBackground="#1000">
3548
<DataGrid.Columns>
@@ -38,9 +51,11 @@
3851
<DataGridTextColumn Header="Region" Binding="{Binding Region}" Width="4*" x:DataType="local:Country" />
3952
<DataGridTextColumn Header="Population" Binding="{Binding Population}" Width="3*" x:DataType="local:Country" />
4053
<DataGridTextColumn Header="Area" Binding="{Binding Area}" Width="3*" x:DataType="local:Country" />
41-
<DataGridTextColumn Header="GDP" Binding="{Binding GDP}" Width="3*"
54+
<DataGridTextColumn Header="GDP" Binding="{Binding GDP}"
55+
Width="{Binding #GDPWidth.Value, Mode=TwoWay, Converter={StaticResource GDPWidthConverter}}"
4256
CellTheme="{StaticResource GdpCell}"
4357
MinWidth="200"
58+
MaxWidth="350"
4459
IsVisible="{Binding #ShowGDP.IsChecked}"
4560
x:DataType="local:Country" />
4661
</DataGrid.Columns>

0 commit comments

Comments
 (0)