This commit is contained in:
Igor Gritsenko 2022-01-24 11:37:17 +03:00
Родитель c6920bacc6
Коммит 022798ae6f
3 изменённых файлов: 135 добавлений и 4 удалений

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

@ -29,5 +29,5 @@ AppBuilder.Configure<Application>()
))
.Title(tb1.GetObservable(TextBox.TextProperty).Select(x => x.ToUpper()).ToBinding());
wnd.Topmost = true;
//wnd.Topmost = true;
}, args);

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

@ -1,14 +1,25 @@
public class HotReloadView : ViewBase
{
protected override object Build() =>
new Grid()
.Background(Brushes.SaddleBrown)
new StackPanel()
.Width(300)
.Children(
new TextBlock()
.Foreground(Brushes.White)
.Padding(12)
.FontSize(30)
.HorizontalAlignment(HorizontalAlignment.Center)
.Text("Hello Hot Reload!")
.Text("Hello Hot Reload!"),
new TextBlock()
.Text("Custom control example:"),
new Border()
.BorderBrush(Brushes.Gray)
.BorderThickness(1)
.Child(
new MyCustomControl()
.Margin(20)
)
);
}

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

@ -0,0 +1,120 @@
public class MyCustomControl : ViewBase
{
#region NewValue Styled Avalonia Property
public string? NewValue
{
get { return GetValue(NewValueProperty); }
set { SetValue(NewValueProperty, value); }
}
public static readonly StyledProperty<string?> NewValueProperty =
AvaloniaProperty.Register<MyCustomControl, string?>
(
nameof(NewValue)
);
#endregion NewValue Styled Avalonia Property
#region SavedValue Styled Avalonia Property
public string? SavedValue
{
get { return GetValue(SavedValueProperty); }
set { SetValue(SavedValueProperty, value); }
}
public static readonly StyledProperty<string?> SavedValueProperty =
AvaloniaProperty.Register<MyCustomControl, string?>
(
nameof(SavedValue)
);
#endregion SavedValue Styled Avalonia Property
#region CanSave Direct Avalonia Property
private bool _canSave = default;
public static readonly DirectProperty<MyCustomControl, bool> CanSaveProperty =
AvaloniaProperty.RegisterDirect<MyCustomControl, bool>
(
nameof(CanSave),
o => o.CanSave
);
public bool CanSave
{
get => _canSave;
private set
{
SetAndRaise(CanSaveProperty, ref _canSave, value);
}
}
#endregion CanSave Direct Avalonia Property
// CanSave is set to true when SavedValue is not the same as NewView
// false otherwise
private void SetCanSave(object? _)
{
CanSave = SavedValue != NewValue;
}
public MyCustomControl()
{
this.GetObservable(NewValueProperty).Subscribe(SetCanSave);
this.GetObservable(SavedValueProperty).Subscribe(SetCanSave);
}
public void Save()
{
SavedValue = NewValue;
}
public void Cancel()
{
NewValue = SavedValue;
}
protected override object Build()
=> new Grid()
.Rows("Auto, Auto, *, Auto")
.Children(
new StackPanel()
.Orientation(Orientation.Horizontal)
.VerticalAlignment(VerticalAlignment.Center)
.Children(
new TextBlock().Text("Enter text:")
.VerticalAlignment(VerticalAlignment.Center),
new TextBox()
.Text(Bind(NewValueProperty))
.MinWidth(150)
),
new StackPanel().Row(1)
.Margin(0, 10)
.Orientation(Orientation.Horizontal)
.VerticalAlignment(VerticalAlignment.Center)
.HorizontalAlignment(HorizontalAlignment.Left)
.Children(
new TextBlock().Text("Saved text:")
.VerticalAlignment(VerticalAlignment.Center),
new TextBox()
.Text(Bind(SavedValueProperty))
.MinWidth(150)
),
new StackPanel().Row(3)
.Orientation(Orientation.Horizontal)
.HorizontalAlignment(HorizontalAlignment.Right)
.Children(
new Button().Content("Cancel")
.Margin(5, 0)
.IsEnabled(Bind(CanSaveProperty))
.Command(Bind(this, "Cancel")),
new Button().Content("Save")
.Margin(5, 0)
.IsEnabled(Bind(CanSaveProperty))
.Command(Bind(this, "Save"))
)
);
}