feat: add temp template.
This commit is contained in:
@@ -1,25 +1,32 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Collections;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml.Templates;
|
||||
using Avalonia.Metadata;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class Form: ItemsControl
|
||||
{
|
||||
|
||||
public static readonly StyledProperty<AvaloniaList<FormItem>> ItemsProperty = AvaloniaProperty.Register<Form, AvaloniaList<FormItem>>(
|
||||
"Items");
|
||||
public static readonly AttachedProperty<string> LabelProperty =
|
||||
AvaloniaProperty.RegisterAttached<Form, Control, string>("Label");
|
||||
public static void SetLabel(Control obj, string value) => obj.SetValue(LabelProperty, value);
|
||||
public static string GetLabel(Control obj) => obj.GetValue(LabelProperty);
|
||||
|
||||
public AvaloniaList<FormItem> Items
|
||||
{
|
||||
get => GetValue(ItemsProperty);
|
||||
set => SetValue(ItemsProperty, value);
|
||||
}
|
||||
|
||||
public void AddChild(object child)
|
||||
public static readonly AttachedProperty<bool> IsRequiredProperty =
|
||||
AvaloniaProperty.RegisterAttached<Form, Control, bool>("IsRequired");
|
||||
public static void SetIsRequired(Control obj, bool value) => obj.SetValue(IsRequiredProperty, value);
|
||||
public static bool GetIsRequired(Control obj) => obj.GetValue(IsRequiredProperty);
|
||||
|
||||
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
recycleKey = null;
|
||||
return item is FormItem or FormGroup;
|
||||
}
|
||||
|
||||
protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
|
||||
{
|
||||
if (item is not Control control) return new FormItem();
|
||||
string label = GetLabel(control);
|
||||
bool isRequired = GetIsRequired(control);
|
||||
return new FormItem() { Label = label, IsRequired = isRequired, Content = control };
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,19 @@ using Avalonia.Controls.Primitives;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class FormGroup: HeaderedItemsControl, IFormItem
|
||||
public class FormGroup: HeaderedItemsControl
|
||||
{
|
||||
|
||||
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
|
||||
{
|
||||
recycleKey = null;
|
||||
return item is not FormItem or FormGroup;
|
||||
}
|
||||
|
||||
protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
|
||||
{
|
||||
if (item is not Control control) return new FormItem();
|
||||
var label = Form.GetLabel(control);
|
||||
var isRequired = Form.GetIsRequired(control);
|
||||
return new FormItem() { Label = label, IsRequired = isRequired, Content = control };
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,27 @@
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class FormItem: TemplatedControl, IFormItem
|
||||
public class FormItem: ContentControl
|
||||
{
|
||||
public static readonly StyledProperty<string> LabelProperty = AvaloniaProperty.Register<FormItem, string>(
|
||||
nameof(Label));
|
||||
|
||||
public string Label
|
||||
{
|
||||
get => GetValue(LabelProperty);
|
||||
set => SetValue(LabelProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<bool> IsRequiredProperty = AvaloniaProperty.Register<FormItem, bool>(
|
||||
nameof(IsRequired));
|
||||
|
||||
public bool IsRequired
|
||||
{
|
||||
get => GetValue(IsRequiredProperty);
|
||||
set => SetValue(IsRequiredProperty, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public interface IFormItem
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user