From 7192673d436acf8e1fb901d6cbd13d4ca163df4e Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 26 Feb 2019 21:53:26 +0100 Subject: [PATCH] Display (broken) data-bound context menu in ControlCatalog. To demonstrate #2325. Moved `MenuPageViewModel` and `MenuItemViewModel` to new `ViewModels` directory so the latter can be reused for `ContextMenuPageViewModel`. --- .../ControlCatalog/Pages/ContextMenuPage.xaml | 22 ++++- .../Pages/ContextMenuPage.xaml.cs | 2 + samples/ControlCatalog/Pages/MenuPage.xaml.cs | 94 +------------------ .../ViewModels/ContextMenuPageViewModel.cs | 73 ++++++++++++++ .../ViewModels/MenuItemViewModel.cs | 13 +++ .../ViewModels/MenuPageViewModel.cs | 89 ++++++++++++++++++ 6 files changed, 199 insertions(+), 94 deletions(-) create mode 100644 samples/ControlCatalog/ViewModels/ContextMenuPageViewModel.cs create mode 100644 samples/ControlCatalog/ViewModels/MenuItemViewModel.cs create mode 100644 samples/ControlCatalog/ViewModels/MenuPageViewModel.cs diff --git a/samples/ControlCatalog/Pages/ContextMenuPage.xaml b/samples/ControlCatalog/Pages/ContextMenuPage.xaml index 932f3292ff..8f147638b5 100644 --- a/samples/ControlCatalog/Pages/ContextMenuPage.xaml +++ b/samples/ControlCatalog/Pages/ContextMenuPage.xaml @@ -10,7 +10,8 @@ HorizontalAlignment="Center" Spacing="16"> + Margin="16" + Padding="48,48,48,48"> @@ -31,7 +32,24 @@ - + + + + + + + + + + + diff --git a/samples/ControlCatalog/Pages/ContextMenuPage.xaml.cs b/samples/ControlCatalog/Pages/ContextMenuPage.xaml.cs index dc73bef07a..96e8b49f89 100644 --- a/samples/ControlCatalog/Pages/ContextMenuPage.xaml.cs +++ b/samples/ControlCatalog/Pages/ContextMenuPage.xaml.cs @@ -1,5 +1,6 @@ using Avalonia.Controls; using Avalonia.Markup.Xaml; +using ControlCatalog.ViewModels; namespace ControlCatalog.Pages { @@ -8,6 +9,7 @@ namespace ControlCatalog.Pages public ContextMenuPage() { this.InitializeComponent(); + DataContext = new ContextMenuPageViewModel(); } private void InitializeComponent() diff --git a/samples/ControlCatalog/Pages/MenuPage.xaml.cs b/samples/ControlCatalog/Pages/MenuPage.xaml.cs index 5a07cae89f..0a77607719 100644 --- a/samples/ControlCatalog/Pages/MenuPage.xaml.cs +++ b/samples/ControlCatalog/Pages/MenuPage.xaml.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using System.Windows.Input; using Avalonia.Controls; using Avalonia.Markup.Xaml; +using ControlCatalog.ViewModels; using ReactiveUI; namespace ControlCatalog.Pages @@ -13,51 +14,7 @@ namespace ControlCatalog.Pages public MenuPage() { this.InitializeComponent(); - var vm = new MenuPageViewModel(); - - vm.MenuItems = new[] - { - new MenuItemViewModel - { - Header = "_File", - Items = new[] - { - new MenuItemViewModel { Header = "_Open...", Command = vm.OpenCommand }, - new MenuItemViewModel { Header = "Save", Command = vm.SaveCommand }, - new MenuItemViewModel { Header = "-" }, - new MenuItemViewModel - { - Header = "Recent", - Items = new[] - { - new MenuItemViewModel - { - Header = "File1.txt", - Command = vm.OpenRecentCommand, - CommandParameter = @"c:\foo\File1.txt" - }, - new MenuItemViewModel - { - Header = "File2.txt", - Command = vm.OpenRecentCommand, - CommandParameter = @"c:\foo\File2.txt" - }, - } - }, - } - }, - new MenuItemViewModel - { - Header = "_Edit", - Items = new[] - { - new MenuItemViewModel { Header = "_Copy" }, - new MenuItemViewModel { Header = "_Paste" }, - } - } - }; - - DataContext = vm; + DataContext = new MenuPageViewModel(); } private void InitializeComponent() @@ -65,51 +22,4 @@ namespace ControlCatalog.Pages AvaloniaXamlLoader.Load(this); } } - - public class MenuPageViewModel - { - public MenuPageViewModel() - { - OpenCommand = ReactiveCommand.CreateFromTask(Open); - SaveCommand = ReactiveCommand.Create(Save); - OpenRecentCommand = ReactiveCommand.Create(OpenRecent); - } - - public IReadOnlyList MenuItems { get; set; } - public ReactiveCommand OpenCommand { get; } - public ReactiveCommand SaveCommand { get; } - public ReactiveCommand OpenRecentCommand { get; } - - public async Task Open() - { - var dialog = new OpenFileDialog(); - var result = await dialog.ShowAsync(App.Current.MainWindow); - - if (result != null) - { - foreach (var path in result) - { - System.Diagnostics.Debug.WriteLine($"Opened: {path}"); - } - } - } - - public void Save() - { - System.Diagnostics.Debug.WriteLine("Save"); - } - - public void OpenRecent(string path) - { - System.Diagnostics.Debug.WriteLine($"Open recent: {path}"); - } - } - - public class MenuItemViewModel - { - public string Header { get; set; } - public ICommand Command { get; set; } - public object CommandParameter { get; set; } - public IList Items { get; set; } - } } diff --git a/samples/ControlCatalog/ViewModels/ContextMenuPageViewModel.cs b/samples/ControlCatalog/ViewModels/ContextMenuPageViewModel.cs new file mode 100644 index 0000000000..d34e9af017 --- /dev/null +++ b/samples/ControlCatalog/ViewModels/ContextMenuPageViewModel.cs @@ -0,0 +1,73 @@ +using System.Collections.Generic; +using System.Reactive; +using System.Threading.Tasks; +using Avalonia.Controls; +using ReactiveUI; + +namespace ControlCatalog.ViewModels +{ + public class ContextMenuPageViewModel + { + public ContextMenuPageViewModel() + { + OpenCommand = ReactiveCommand.CreateFromTask(Open); + SaveCommand = ReactiveCommand.Create(Save); + OpenRecentCommand = ReactiveCommand.Create(OpenRecent); + + MenuItems = new[] + { + new MenuItemViewModel { Header = "_Open...", Command = OpenCommand }, + new MenuItemViewModel { Header = "Save", Command = SaveCommand }, + new MenuItemViewModel { Header = "-" }, + new MenuItemViewModel + { + Header = "Recent", + Items = new[] + { + new MenuItemViewModel + { + Header = "File1.txt", + Command = OpenRecentCommand, + CommandParameter = @"c:\foo\File1.txt" + }, + new MenuItemViewModel + { + Header = "File2.txt", + Command = OpenRecentCommand, + CommandParameter = @"c:\foo\File2.txt" + }, + } + }, + }; + } + + public IReadOnlyList MenuItems { get; set; } + public ReactiveCommand OpenCommand { get; } + public ReactiveCommand SaveCommand { get; } + public ReactiveCommand OpenRecentCommand { get; } + + public async Task Open() + { + var dialog = new OpenFileDialog(); + var result = await dialog.ShowAsync(App.Current.MainWindow); + + if (result != null) + { + foreach (var path in result) + { + System.Diagnostics.Debug.WriteLine($"Opened: {path}"); + } + } + } + + public void Save() + { + System.Diagnostics.Debug.WriteLine("Save"); + } + + public void OpenRecent(string path) + { + System.Diagnostics.Debug.WriteLine($"Open recent: {path}"); + } + } +} diff --git a/samples/ControlCatalog/ViewModels/MenuItemViewModel.cs b/samples/ControlCatalog/ViewModels/MenuItemViewModel.cs new file mode 100644 index 0000000000..1e4a764efc --- /dev/null +++ b/samples/ControlCatalog/ViewModels/MenuItemViewModel.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; +using System.Windows.Input; + +namespace ControlCatalog.ViewModels +{ + public class MenuItemViewModel + { + public string Header { get; set; } + public ICommand Command { get; set; } + public object CommandParameter { get; set; } + public IList Items { get; set; } + } +} diff --git a/samples/ControlCatalog/ViewModels/MenuPageViewModel.cs b/samples/ControlCatalog/ViewModels/MenuPageViewModel.cs new file mode 100644 index 0000000000..038f3574cc --- /dev/null +++ b/samples/ControlCatalog/ViewModels/MenuPageViewModel.cs @@ -0,0 +1,89 @@ +using System.Collections.Generic; +using System.Reactive; +using System.Threading.Tasks; +using Avalonia.Controls; +using ReactiveUI; + +namespace ControlCatalog.ViewModels +{ + public class MenuPageViewModel + { + public MenuPageViewModel() + { + OpenCommand = ReactiveCommand.CreateFromTask(Open); + SaveCommand = ReactiveCommand.Create(Save); + OpenRecentCommand = ReactiveCommand.Create(OpenRecent); + + MenuItems = new[] + { + new MenuItemViewModel + { + Header = "_File", + Items = new[] + { + new MenuItemViewModel { Header = "_Open...", Command = OpenCommand }, + new MenuItemViewModel { Header = "Save", Command = SaveCommand }, + new MenuItemViewModel { Header = "-" }, + new MenuItemViewModel + { + Header = "Recent", + Items = new[] + { + new MenuItemViewModel + { + Header = "File1.txt", + Command = OpenRecentCommand, + CommandParameter = @"c:\foo\File1.txt" + }, + new MenuItemViewModel + { + Header = "File2.txt", + Command = OpenRecentCommand, + CommandParameter = @"c:\foo\File2.txt" + }, + } + }, + } + }, + new MenuItemViewModel + { + Header = "_Edit", + Items = new[] + { + new MenuItemViewModel { Header = "_Copy" }, + new MenuItemViewModel { Header = "_Paste" }, + } + } + }; + } + + public IReadOnlyList MenuItems { get; set; } + public ReactiveCommand OpenCommand { get; } + public ReactiveCommand SaveCommand { get; } + public ReactiveCommand OpenRecentCommand { get; } + + public async Task Open() + { + var dialog = new OpenFileDialog(); + var result = await dialog.ShowAsync(App.Current.MainWindow); + + if (result != null) + { + foreach (var path in result) + { + System.Diagnostics.Debug.WriteLine($"Opened: {path}"); + } + } + } + + public void Save() + { + System.Diagnostics.Debug.WriteLine("Save"); + } + + public void OpenRecent(string path) + { + System.Diagnostics.Debug.WriteLine($"Open recent: {path}"); + } + } +}