feat: initialize, setup demo. make sure MVVM works.
This commit is contained in:
48
demo/Ursa.Demo/DataTemplates/ToolBarItemTemplateSelector.cs
Normal file
48
demo/Ursa.Demo/DataTemplates/ToolBarItemTemplateSelector.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Avalonia.Data;
|
||||
using Ursa.Demo.ViewModels;
|
||||
|
||||
namespace Ursa.Demo.Converters;
|
||||
|
||||
public class ToolBarItemTemplateSelector: IDataTemplate
|
||||
{
|
||||
public static ToolBarItemTemplateSelector Instance { get; } = new();
|
||||
public Control? Build(object? param)
|
||||
{
|
||||
if (param is null) return null;
|
||||
if (param is ToolBarButtonItemViewModel vm)
|
||||
{
|
||||
return new Button()
|
||||
{
|
||||
[!ContentControl.ContentProperty] = new Binding() { Path = "Content" },
|
||||
[!Button.CommandProperty] = new Binding() { Path = "Command" },
|
||||
};
|
||||
}
|
||||
if (param is ToolBarCheckBoxItemViweModel cb)
|
||||
{
|
||||
return new CheckBox()
|
||||
{
|
||||
[!ContentControl.ContentProperty] = new Binding() { Path = "Content" },
|
||||
[!ToggleButton.IsCheckedProperty] = new Binding() { Path = "IsChecked" },
|
||||
[!Button.CommandProperty] = new Binding() { Path = "Command" },
|
||||
};
|
||||
}
|
||||
if (param is ToolBarComboBoxItemViewModel combo)
|
||||
{
|
||||
return new ComboBox()
|
||||
{
|
||||
[!ContentControl.ContentProperty] = new Binding() { Path = "Content" },
|
||||
[!SelectingItemsControl.SelectedItemProperty] = new Binding() { Path = "SelectedItem" },
|
||||
[!ItemsControl.ItemsSourceProperty] = new Binding() { Path = "Items" },
|
||||
};
|
||||
}
|
||||
return new Button() { Content = "Undefined Item" };
|
||||
}
|
||||
|
||||
public bool Match(object? data)
|
||||
{
|
||||
return data is ToolBarItemViewModel;
|
||||
}
|
||||
}
|
||||
@@ -31,5 +31,6 @@ public static class MenuKeys
|
||||
public const string MenuKeyTimeline = "Timeline";
|
||||
public const string MenuKeyTwoTonePathIcon = "TwoTonePathIcon";
|
||||
public const string MenuKeyThemeToggler = "ThemeToggler";
|
||||
public const string MenuKeyToolBar = "ToolBar";
|
||||
|
||||
}
|
||||
24
demo/Ursa.Demo/Pages/ToolBarDemo.axaml
Normal file
24
demo/Ursa.Demo/Pages/ToolBarDemo.axaml
Normal file
@@ -0,0 +1,24 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:u="https://irihi.tech/ursa"
|
||||
xmlns:vm="using:Ursa.Demo.ViewModels"
|
||||
xmlns:template="using:Ursa.Demo.Converters"
|
||||
x:DataType="vm:ToolBarDemoViewModel"
|
||||
x:CompileBindings="True"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="Ursa.Demo.Pages.ToolBarDemo">
|
||||
<StackPanel>
|
||||
<u:ToolBar>
|
||||
<Button Content="Button 1" />
|
||||
<Button Content="Button 2" />
|
||||
<Button Content="Button 3" />
|
||||
</u:ToolBar>
|
||||
<u:ToolBar ItemsSource="{Binding Items}">
|
||||
<u:ToolBar.ItemTemplate>
|
||||
<template:ToolBarItemTemplateSelector/>
|
||||
</u:ToolBar.ItemTemplate>
|
||||
</u:ToolBar>
|
||||
</StackPanel>
|
||||
</UserControl>
|
||||
13
demo/Ursa.Demo/Pages/ToolBarDemo.axaml.cs
Normal file
13
demo/Ursa.Demo/Pages/ToolBarDemo.axaml.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
|
||||
namespace Ursa.Demo.Pages;
|
||||
|
||||
public partial class ToolBarDemo : UserControl
|
||||
{
|
||||
public ToolBarDemo()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,7 @@ public class MainViewViewModel : ViewModelBase
|
||||
MenuKeys.MenuKeyTimeline => new TimelineDemoViewModel(),
|
||||
MenuKeys.MenuKeyTwoTonePathIcon => new TwoTonePathIconDemoViewModel(),
|
||||
MenuKeys.MenuKeyThemeToggler => new ThemeTogglerDemoViewModel(),
|
||||
MenuKeys.MenuKeyToolBar => new ToolBarDemoViewModel(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,7 @@ public class MenuViewModel: ViewModelBase
|
||||
new() { MenuHeader = "Theme Toggler", Key = MenuKeys.MenuKeyThemeToggler },
|
||||
new() { MenuHeader = "Timeline", Key = MenuKeys.MenuKeyTimeline, Status = "Updated" },
|
||||
new() { MenuHeader = "TwoTonePathIcon", Key = MenuKeys.MenuKeyTwoTonePathIcon, Status = "New"},
|
||||
new() { MenuHeader = "ToolBar", Key = MenuKeys.MenuKeyToolBar, Status = "New" }
|
||||
};
|
||||
}
|
||||
}
|
||||
72
demo/Ursa.Demo/ViewModels/ToolBarDemoViewModel.cs
Normal file
72
demo/Ursa.Demo/ViewModels/ToolBarDemoViewModel.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows.Input;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Ursa.Controls;
|
||||
|
||||
namespace Ursa.Demo.ViewModels;
|
||||
|
||||
public partial class ToolBarDemoViewModel: ObservableObject
|
||||
{
|
||||
public ObservableCollection<ToolBarItemViewModel> Items { get; set; }
|
||||
public ToolBarDemoViewModel()
|
||||
{
|
||||
Items = new()
|
||||
{
|
||||
new ToolBarButtonItemViewModel() { Content = "New" },
|
||||
new ToolBarButtonItemViewModel() { Content = "Open" },
|
||||
new ToolBarButtonItemViewModel() { Content = "Save" },
|
||||
new ToolBarCheckBoxItemViweModel() { Content = "Bold" },
|
||||
new ToolBarCheckBoxItemViweModel() { Content = "Italic" },
|
||||
new ToolBarComboBoxItemViewModel() { Content = "Font Size", Items = new (){ "10", "12", "14" } }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class ToolBarItemViewModel: ObservableObject
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class ToolBarButtonItemViewModel: ToolBarItemViewModel
|
||||
{
|
||||
public string Content { get; set; }
|
||||
public ICommand Command { get; set; }
|
||||
|
||||
public ToolBarButtonItemViewModel()
|
||||
{
|
||||
Command = new AsyncRelayCommand(async () => { await MessageBox.ShowOverlayAsync(Content); });
|
||||
}
|
||||
}
|
||||
|
||||
public class ToolBarCheckBoxItemViweModel: ToolBarItemViewModel
|
||||
{
|
||||
public string Content { get; set; }
|
||||
public bool IsChecked { get; set; }
|
||||
public ICommand Command { get; set; }
|
||||
|
||||
public ToolBarCheckBoxItemViweModel()
|
||||
{
|
||||
Command = new AsyncRelayCommand(async () => { await MessageBox.ShowOverlayAsync(Content); });
|
||||
}
|
||||
}
|
||||
|
||||
public class ToolBarComboBoxItemViewModel: ToolBarItemViewModel
|
||||
{
|
||||
public string Content { get; set; }
|
||||
public ObservableCollection<string> Items { get; set; }
|
||||
|
||||
private string _selectedItem;
|
||||
public string SelectedItem
|
||||
{
|
||||
get => _selectedItem;
|
||||
set
|
||||
{
|
||||
SetProperty(ref _selectedItem, value);
|
||||
MessageBox.ShowOverlayAsync(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user