This commit is contained in:
Wiesław Šoltés 2021-05-18 15:33:52 +02:00
Родитель d8eb54cf22
Коммит b592f998bd
11 изменённых файлов: 317 добавлений и 318 удалений

Просмотреть файл

@ -2,6 +2,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="GridDemo.App">
<Application.Styles>
<FluentTheme Mode="Light"/>
<FluentTheme Mode="Light" />
</Application.Styles>
</Application>
</Application>

Просмотреть файл

@ -4,21 +4,21 @@ using Avalonia.Markup.Xaml;
namespace GridDemo
{
public class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow();
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow();
}
base.OnFrameworkInitializationCompleted();
}
}
base.OnFrameworkInitializationCompleted();
}
}
}

Просмотреть файл

@ -4,57 +4,57 @@ using Avalonia.Controls;
namespace GridDemo.Controls
{
public class BindableGrid : Grid
{
public static readonly StyledProperty<string?> ColumnDefinitionsSourceProperty =
AvaloniaProperty.Register<BindableGrid, string?>(nameof(ColumnDefinitionsSource));
public class BindableGrid : Grid
{
public static readonly StyledProperty<string?> ColumnDefinitionsSourceProperty =
AvaloniaProperty.Register<BindableGrid, string?>(nameof(ColumnDefinitionsSource));
public static readonly StyledProperty<string?> RowDefinitionsSourceProperty =
AvaloniaProperty.Register<BindableGrid, string?>(nameof(RowDefinitionsSource));
public static readonly StyledProperty<string?> RowDefinitionsSourceProperty =
AvaloniaProperty.Register<BindableGrid, string?>(nameof(RowDefinitionsSource));
public BindableGrid()
{
InvalidateDefinitions();
}
public BindableGrid()
{
InvalidateDefinitions();
}
public string? ColumnDefinitionsSource
{
get => GetValue(ColumnDefinitionsSourceProperty);
set => SetValue(ColumnDefinitionsSourceProperty, value);
}
public string? ColumnDefinitionsSource
{
get => GetValue(ColumnDefinitionsSourceProperty);
set => SetValue(ColumnDefinitionsSourceProperty, value);
}
public string? RowDefinitionsSource
{
get => GetValue(RowDefinitionsSourceProperty);
set => SetValue(RowDefinitionsSourceProperty, value);
}
public string? RowDefinitionsSource
{
get => GetValue(RowDefinitionsSourceProperty);
set => SetValue(RowDefinitionsSourceProperty, value);
}
protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change)
{
base.OnPropertyChanged(change);
protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> change)
{
base.OnPropertyChanged(change);
if (change.Property == ColumnDefinitionsSourceProperty
|| change.Property == RowDefinitionsSourceProperty)
{
InvalidateDefinitions();
}
}
if (change.Property == ColumnDefinitionsSourceProperty
|| change.Property == RowDefinitionsSourceProperty)
{
InvalidateDefinitions();
}
}
private void InvalidateDefinitions()
{
if (ColumnDefinitionsSource is not null && RowDefinitionsSource is not null)
{
var columns = GridLength.ParseLengths(ColumnDefinitionsSource).Select(x => new ColumnDefinition(x));
ColumnDefinitions.Clear();
ColumnDefinitions.AddRange(columns);
private void InvalidateDefinitions()
{
if (ColumnDefinitionsSource is not null && RowDefinitionsSource is not null)
{
var columns = GridLength.ParseLengths(ColumnDefinitionsSource).Select(x => new ColumnDefinition(x));
ColumnDefinitions.Clear();
ColumnDefinitions.AddRange(columns);
var rows = GridLength.ParseLengths(RowDefinitionsSource).Select(x => new RowDefinition(x));
RowDefinitions.Clear();
RowDefinitions.AddRange(rows);
var rows = GridLength.ParseLengths(RowDefinitionsSource).Select(x => new RowDefinition(x));
RowDefinitions.Clear();
RowDefinitions.AddRange(rows);
InvalidateMeasure();
InvalidateArrange();
}
}
}
InvalidateMeasure();
InvalidateArrange();
}
}
}
}

Просмотреть файл

@ -10,17 +10,17 @@
x:CompileBindings="True" x:DataType="vm:MainWindowViewModel"
Title="GridDemo">
<Window.DataContext>
<vm:MainWindowViewModel/>
<vm:MainWindowViewModel />
</Window.DataContext>
<DockPanel>
<ComboBox DockPanel.Dock="Left"
<ComboBox DockPanel.Dock="Left"
Items="{Binding Layouts}"
SelectedIndex="{Binding LayoutIndex}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
@ -36,17 +36,17 @@
<!-- <Setter Property="Grid.Row" Value="{Binding CurrentTilePreset.Row}"/> -->
<!-- <Setter Property="Grid.ColumnSpan" Value="{Binding CurrentTilePreset.ColumnSpan}"/> -->
<!-- <Setter Property="Grid.RowSpan" Value="{Binding CurrentTilePreset.RowSpan}"/> -->
<Setter Property="Grid.Column" Value="{Binding Column}"/>
<Setter Property="Grid.Row" Value="{Binding Row}"/>
<Setter Property="Grid.ColumnSpan" Value="{Binding ColumnSpan}"/>
<Setter Property="Grid.RowSpan" Value="{Binding RowSpan}"/>
<Setter Property="Grid.Column" Value="{Binding Column}" />
<Setter Property="Grid.Row" Value="{Binding Row}" />
<Setter Property="Grid.ColumnSpan" Value="{Binding ColumnSpan}" />
<Setter Property="Grid.RowSpan" Value="{Binding RowSpan}" />
</Style>
</ItemsControl.Styles>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<controls:BindableGrid ColumnDefinitionsSource="{Binding CurrentLayout.ColumnDefinitions}"
RowDefinitionsSource="{Binding CurrentLayout.RowDefinitions}"
ShowGridLines="False"/>
ShowGridLines="False" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.DataTemplates>
@ -56,19 +56,18 @@
Background="Gray"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock Text="{Binding $parent[ContentPresenter].(Grid.Column)}"/>
<TextBlock Text="{Binding $parent[ContentPresenter].(Grid.Row)}"/>
<TextBlock Text="{Binding $parent[ContentPresenter].(Grid.ColumnSpan)}"/>
<TextBlock Text="{Binding $parent[ContentPresenter].(Grid.RowSpan)}"/>
<TextBlock Text="{Binding $parent[ContentPresenter].(Grid.Column)}" />
<TextBlock Text="{Binding $parent[ContentPresenter].(Grid.Row)}" />
<TextBlock Text="{Binding $parent[ContentPresenter].(Grid.ColumnSpan)}" />
<TextBlock Text="{Binding $parent[ContentPresenter].(Grid.RowSpan)}" />
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.DataTemplates>
</ItemsControl>
</ScrollViewer>
</DockPanel>
</DockPanel>
</Window>

Просмотреть файл

@ -4,19 +4,19 @@ using Avalonia.Markup.Xaml;
namespace GridDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
#if DEBUG
this.AttachDevTools();
this.AttachDevTools();
#endif
}
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
}
}

Просмотреть файл

@ -6,19 +6,19 @@ using Avalonia.ReactiveUI;
namespace GridDemo
{
class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.UseReactiveUI()
.LogToTrace();
}
// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.UseReactiveUI()
.LogToTrace();
}
}

Просмотреть файл

@ -6,124 +6,124 @@ using ReactiveUI;
namespace GridDemo.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
private ObservableCollection<TileItem>? _tiles;
private ObservableCollection<TileLayout>? _layouts;
private int _layoutIndex;
public class MainWindowViewModel : ViewModelBase
{
private ObservableCollection<TileItem>? _tiles;
private ObservableCollection<TileLayout>? _layouts;
private int _layoutIndex;
public ObservableCollection<TileItem>? Tiles
{
get => _tiles;
set => this.RaiseAndSetIfChanged(ref _tiles, value);
}
public ObservableCollection<TileItem>? Tiles
{
get => _tiles;
set => this.RaiseAndSetIfChanged(ref _tiles, value);
}
public ObservableCollection<TileLayout>? Layouts
{
get => _layouts;
set => this.RaiseAndSetIfChanged(ref _layouts, value);
}
public ObservableCollection<TileLayout>? Layouts
{
get => _layouts;
set => this.RaiseAndSetIfChanged(ref _layouts, value);
}
public int LayoutIndex
{
get => _layoutIndex;
set
{
this.RaiseAndSetIfChanged(ref _layoutIndex, value);
this.RaisePropertyChanged(nameof(CurrentLayout));
}
}
public int LayoutIndex
{
get => _layoutIndex;
set
{
this.RaiseAndSetIfChanged(ref _layoutIndex, value);
this.RaisePropertyChanged(nameof(CurrentLayout));
}
}
public TileLayout? CurrentLayout => Layouts?[LayoutIndex];
public TileLayout? CurrentLayout => Layouts?[LayoutIndex];
public MainWindowViewModel()
{
LayoutIndex = 2;
public MainWindowViewModel()
{
LayoutIndex = 2;
Layouts = new ObservableCollection<TileLayout>()
{
new TileLayout("Small", "228,228,228,228,228", "126"),
new TileLayout("Normal", "228,228,228", "126,252"),
new TileLayout("Wide", "228,228", "126,252,252"),
};
Layouts = new ObservableCollection<TileLayout>()
{
new TileLayout("Small", "228,228,228,228,228", "126"),
new TileLayout("Normal", "228,228,228", "126,252"),
new TileLayout("Wide", "228,228", "126,252,252"),
};
Tiles = new ObservableCollection<TileItem>()
{
// 0
new TileItem()
{
Background = Brushes.Red,
TilePresets = new ObservableCollection<TilePreset>()
{
new TilePreset(0, 0, 1, 1),
new TilePreset(0, 0, 1, 1),
new TilePreset(0, 0, 1, 1),
},
TilePresetIndex = LayoutIndex
},
// 1
new TileItem()
{
Background = Brushes.Green,
TilePresets = new ObservableCollection<TilePreset>()
{
new TilePreset(1, 0, 1, 1),
new TilePreset(1, 0, 1, 1),
new TilePreset(1, 0, 1, 1),
},
TilePresetIndex = LayoutIndex
},
// 2
new TileItem()
{
Background = Brushes.Blue,
TilePresets = new ObservableCollection<TilePreset>()
{
new TilePreset(2, 0, 1, 1),
new TilePreset(2, 0, 1, 1),
new TilePreset(0, 1, 1, 1),
},
TilePresetIndex = LayoutIndex
},
// 3
new TileItem()
{
Background = Brushes.Yellow,
TilePresets = new ObservableCollection<TilePreset>()
{
new TilePreset(3, 0, 1, 1),
new TilePreset(0, 1, 1, 1),
new TilePreset(1, 1, 1, 1),
},
TilePresetIndex = LayoutIndex
},
// 4
new TileItem()
{
Background = Brushes.Black,
TilePresets = new ObservableCollection<TilePreset>()
{
new TilePreset(4, 0, 1, 1),
new TilePreset(1, 1, 2, 1),
new TilePreset(0, 2, 2, 1),
},
TilePresetIndex = LayoutIndex
},
};
Tiles = new ObservableCollection<TileItem>()
{
// 0
new TileItem()
{
Background = Brushes.Red,
TilePresets = new ObservableCollection<TilePreset>()
{
new TilePreset(0, 0, 1, 1),
new TilePreset(0, 0, 1, 1),
new TilePreset(0, 0, 1, 1),
},
TilePresetIndex = LayoutIndex
},
// 1
new TileItem()
{
Background = Brushes.Green,
TilePresets = new ObservableCollection<TilePreset>()
{
new TilePreset(1, 0, 1, 1),
new TilePreset(1, 0, 1, 1),
new TilePreset(1, 0, 1, 1),
},
TilePresetIndex = LayoutIndex
},
// 2
new TileItem()
{
Background = Brushes.Blue,
TilePresets = new ObservableCollection<TilePreset>()
{
new TilePreset(2, 0, 1, 1),
new TilePreset(2, 0, 1, 1),
new TilePreset(0, 1, 1, 1),
},
TilePresetIndex = LayoutIndex
},
// 3
new TileItem()
{
Background = Brushes.Yellow,
TilePresets = new ObservableCollection<TilePreset>()
{
new TilePreset(3, 0, 1, 1),
new TilePreset(0, 1, 1, 1),
new TilePreset(1, 1, 1, 1),
},
TilePresetIndex = LayoutIndex
},
// 4
new TileItem()
{
Background = Brushes.Black,
TilePresets = new ObservableCollection<TilePreset>()
{
new TilePreset(4, 0, 1, 1),
new TilePreset(1, 1, 2, 1),
new TilePreset(0, 2, 2, 1),
},
TilePresetIndex = LayoutIndex
},
};
this.WhenAnyValue(x => x.LayoutIndex)
.Subscribe(_ => UpdateTiles());
}
this.WhenAnyValue(x => x.LayoutIndex)
.Subscribe(_ => UpdateTiles());
}
private void UpdateTiles()
{
if (Tiles != null)
{
foreach (var tile in Tiles)
{
tile.TilePresetIndex = LayoutIndex;
}
}
}
}
private void UpdateTiles()
{
if (Tiles != null)
{
foreach (var tile in Tiles)
{
tile.TilePresetIndex = LayoutIndex;
}
}
}
}
}

Просмотреть файл

@ -5,48 +5,48 @@ using ReactiveUI;
namespace GridDemo.ViewModels.TileControl
{
public class TileItem : ViewModelBase
{
private ObservableCollection<TilePreset>? _tilePresets;
private int _tilePresetIndex;
public class TileItem : ViewModelBase
{
private ObservableCollection<TilePreset>? _tilePresets;
private int _tilePresetIndex;
public ObservableCollection<TilePreset>? TilePresets
{
get => _tilePresets;
set => this.RaiseAndSetIfChanged(ref _tilePresets, value);
}
public ObservableCollection<TilePreset>? TilePresets
{
get => _tilePresets;
set => this.RaiseAndSetIfChanged(ref _tilePresets, value);
}
public int TilePresetIndex
{
get => _tilePresetIndex;
set => this.RaiseAndSetIfChanged(ref _tilePresetIndex, value);
}
public int TilePresetIndex
{
get => _tilePresetIndex;
set => this.RaiseAndSetIfChanged(ref _tilePresetIndex, value);
}
public TileItem()
{
this.WhenAnyValue(x => x.TilePresetIndex)
.Subscribe(_ => NotifyPresetChanged());
}
public TileItem()
{
this.WhenAnyValue(x => x.TilePresetIndex)
.Subscribe(_ => NotifyPresetChanged());
}
public int Column => CurrentTilePreset?.Column ?? 0;
public int Column => CurrentTilePreset?.Column ?? 0;
public int Row => CurrentTilePreset?.Row ?? 0;
public int Row => CurrentTilePreset?.Row ?? 0;
public int ColumnSpan => CurrentTilePreset?.ColumnSpan ?? 1;
public int ColumnSpan => CurrentTilePreset?.ColumnSpan ?? 1;
public int RowSpan => CurrentTilePreset?.RowSpan ?? 1;
public int RowSpan => CurrentTilePreset?.RowSpan ?? 1;
public TilePreset? CurrentTilePreset => TilePresets?[TilePresetIndex];
public TilePreset? CurrentTilePreset => TilePresets?[TilePresetIndex];
public IBrush? Background { get; set; }
public IBrush? Background { get; set; }
private void NotifyPresetChanged()
{
this.RaisePropertyChanged(nameof(CurrentTilePreset));
this.RaisePropertyChanged(nameof(Column));
this.RaisePropertyChanged(nameof(Row));
this.RaisePropertyChanged(nameof(ColumnSpan));
this.RaisePropertyChanged(nameof(RowSpan));
}
}
private void NotifyPresetChanged()
{
this.RaisePropertyChanged(nameof(CurrentTilePreset));
this.RaisePropertyChanged(nameof(Column));
this.RaisePropertyChanged(nameof(Row));
this.RaisePropertyChanged(nameof(ColumnSpan));
this.RaisePropertyChanged(nameof(RowSpan));
}
}
}

Просмотреть файл

@ -2,39 +2,39 @@ using ReactiveUI;
namespace GridDemo.ViewModels.TileControl
{
public class TileLayout : ViewModelBase
{
private string? _name;
private string? _columnDefinitions;
private string? _rowDefinitions;
public class TileLayout : ViewModelBase
{
private string? _name;
private string? _columnDefinitions;
private string? _rowDefinitions;
public string? Name
{
get => _name;
set => this.RaiseAndSetIfChanged(ref _name, value);
}
public string? Name
{
get => _name;
set => this.RaiseAndSetIfChanged(ref _name, value);
}
public string? ColumnDefinitions
{
get => _columnDefinitions;
set => this.RaiseAndSetIfChanged(ref _columnDefinitions, value);
}
public string? ColumnDefinitions
{
get => _columnDefinitions;
set => this.RaiseAndSetIfChanged(ref _columnDefinitions, value);
}
public string? RowDefinitions
{
get => _rowDefinitions;
set => this.RaiseAndSetIfChanged(ref _rowDefinitions, value);
}
public string? RowDefinitions
{
get => _rowDefinitions;
set => this.RaiseAndSetIfChanged(ref _rowDefinitions, value);
}
public TileLayout()
{
}
public TileLayout()
{
}
public TileLayout(string name, string columnDefinitions, string rowDefinitions)
{
Name = name;
ColumnDefinitions = columnDefinitions;
RowDefinitions = rowDefinitions;
}
}
public TileLayout(string name, string columnDefinitions, string rowDefinitions)
{
Name = name;
ColumnDefinitions = columnDefinitions;
RowDefinitions = rowDefinitions;
}
}
}

Просмотреть файл

@ -2,47 +2,47 @@ using ReactiveUI;
namespace GridDemo.ViewModels.TileControl
{
public class TilePreset : ViewModelBase
{
private int _column;
private int _row;
private int _columnSpan;
private int _rowSpan;
public class TilePreset : ViewModelBase
{
private int _column;
private int _row;
private int _columnSpan;
private int _rowSpan;
public int Column
{
get => _column;
set => this.RaiseAndSetIfChanged(ref _column, value);
}
public int Column
{
get => _column;
set => this.RaiseAndSetIfChanged(ref _column, value);
}
public int Row
{
get => _row;
set => this.RaiseAndSetIfChanged(ref _row, value);
}
public int Row
{
get => _row;
set => this.RaiseAndSetIfChanged(ref _row, value);
}
public int ColumnSpan
{
get => _columnSpan;
set => this.RaiseAndSetIfChanged(ref _columnSpan, value);
}
public int ColumnSpan
{
get => _columnSpan;
set => this.RaiseAndSetIfChanged(ref _columnSpan, value);
}
public int RowSpan
{
get => _rowSpan;
set => this.RaiseAndSetIfChanged(ref _rowSpan, value);
}
public int RowSpan
{
get => _rowSpan;
set => this.RaiseAndSetIfChanged(ref _rowSpan, value);
}
public TilePreset()
{
}
public TilePreset()
{
}
public TilePreset(int column, int row, int columnSpan, int rowSpan)
{
Column = column;
Row = row;
ColumnSpan = columnSpan;
RowSpan = rowSpan;
}
}
public TilePreset(int column, int row, int columnSpan, int rowSpan)
{
Column = column;
Row = row;
ColumnSpan = columnSpan;
RowSpan = rowSpan;
}
}
}

Просмотреть файл

@ -2,7 +2,7 @@ using ReactiveUI;
namespace GridDemo.ViewModels
{
public class ViewModelBase : ReactiveObject
{
}
public class ViewModelBase : ReactiveObject
{
}
}