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" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="GridDemo.App"> x:Class="GridDemo.App">
<Application.Styles> <Application.Styles>
<FluentTheme Mode="Light"/> <FluentTheme Mode="Light" />
</Application.Styles> </Application.Styles>
</Application> </Application>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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