feat: initialize, setup demo. make sure MVVM works.
This commit is contained in:
26
demo/Ursa.Demo/DataTemplates/MenuDataTemplateSelector.cs
Normal file
26
demo/Ursa.Demo/DataTemplates/MenuDataTemplateSelector.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Ursa.Demo.ViewModels;
|
||||
|
||||
namespace Ursa.Demo.Converters;
|
||||
|
||||
public class MenuDataTemplateSelector: IDataTemplate
|
||||
{
|
||||
public IDataTemplate? MenuTemplate { get; set; }
|
||||
public IDataTemplate? SeparatorTemplate { get; set; }
|
||||
|
||||
public Control? Build(object? param)
|
||||
{
|
||||
if (param is MenuItemViewModel vm)
|
||||
{
|
||||
if (vm.IsSeparator) return SeparatorTemplate?.Build(vm);
|
||||
else return MenuTemplate?.Build(vm);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool Match(object? data)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
29
demo/Ursa.Demo/DataTemplates/ViewLocator.cs
Normal file
29
demo/Ursa.Demo/DataTemplates/ViewLocator.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Ursa.Demo.Pages;
|
||||
|
||||
namespace Ursa.Demo.Converters;
|
||||
|
||||
public class ViewLocator: IDataTemplate
|
||||
{
|
||||
public Control? Build(object? param)
|
||||
{
|
||||
if (param is null) return null;
|
||||
var name = param.GetType().Name!.Replace("ViewModel", "");
|
||||
var type = Type.GetType("Ursa.Demo.Pages."+name);
|
||||
if (type != null)
|
||||
{
|
||||
return (Control)Activator.CreateInstance(type)!;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new TextBlock { Text = "Not Found: " + name };
|
||||
}
|
||||
}
|
||||
|
||||
public bool Match(object? data)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user