diff --git a/src/Avalonia.Controls/Control.cs b/src/Avalonia.Controls/Control.cs index 41370d8464..620f5afa81 100644 --- a/src/Avalonia.Controls/Control.cs +++ b/src/Avalonia.Controls/Control.cs @@ -8,6 +8,8 @@ using Avalonia.Rendering; using Avalonia.Styling; using Avalonia.VisualTree; +#nullable enable + namespace Avalonia.Controls { /// @@ -23,20 +25,20 @@ namespace Avalonia.Controls /// /// Defines the property. /// - public static readonly StyledProperty> FocusAdornerProperty = - AvaloniaProperty.Register>(nameof(FocusAdorner)); + public static readonly StyledProperty?> FocusAdornerProperty = + AvaloniaProperty.Register?>(nameof(FocusAdorner)); /// /// Defines the property. /// - public static readonly StyledProperty TagProperty = - AvaloniaProperty.Register(nameof(Tag)); + public static readonly StyledProperty TagProperty = + AvaloniaProperty.Register(nameof(Tag)); /// /// Defines the property. /// - public static readonly StyledProperty ContextMenuProperty = - AvaloniaProperty.Register(nameof(ContextMenu)); + public static readonly StyledProperty ContextMenuProperty = + AvaloniaProperty.Register(nameof(ContextMenu)); /// /// Event raised when an element wishes to be scrolled into view. @@ -44,16 +46,16 @@ namespace Avalonia.Controls public static readonly RoutedEvent RequestBringIntoViewEvent = RoutedEvent.Register("RequestBringIntoView", RoutingStrategies.Bubble); - private DataTemplates _dataTemplates; - private IControl _focusAdorner; + private DataTemplates? _dataTemplates; + private IControl? _focusAdorner; /// /// Gets or sets the control's focus adorner. /// - public ITemplate FocusAdorner + public ITemplate? FocusAdorner { - get { return GetValue(FocusAdornerProperty); } - set { SetValue(FocusAdornerProperty, value); } + get => GetValue(FocusAdornerProperty); + set => SetValue(FocusAdornerProperty, value); } /// @@ -63,27 +65,27 @@ namespace Avalonia.Controls /// Each control may define data templates which are applied to the control itself and its /// children. /// - public DataTemplates DataTemplates => _dataTemplates ?? (_dataTemplates = new DataTemplates()); + public DataTemplates DataTemplates => _dataTemplates ??= new DataTemplates(); /// /// Gets or sets a context menu to the control. /// - public ContextMenu ContextMenu + public ContextMenu? ContextMenu { - get { return GetValue(ContextMenuProperty); } - set { SetValue(ContextMenuProperty, value); } + get => GetValue(ContextMenuProperty); + set => SetValue(ContextMenuProperty, value); } /// /// Gets or sets a user-defined object attached to the control. /// - public object Tag + public object? Tag { - get { return GetValue(TagProperty); } - set { SetValue(TagProperty, value); } + get => GetValue(TagProperty); + set => SetValue(TagProperty, value); } - public new IControl Parent => (IControl)base.Parent; + public new IControl? Parent => (IControl?)base.Parent; /// bool IDataTemplateHost.IsDataTemplatesInitialized => _dataTemplates != null; @@ -106,15 +108,10 @@ namespace Avalonia.Controls { var c = i as IControl; - if (c?.IsInitialized == false) + if (c?.IsInitialized == false && c is ISupportInitialize init) { - var init = c as ISupportInitialize; - - if (init != null) - { - init.BeginInit(); - init.EndInit(); - } + init.BeginInit(); + init.EndInit(); } } } @@ -131,10 +128,7 @@ namespace Avalonia.Controls /// Gets the element that receives the focus adorner. /// /// The control that receives the focus adorner. - protected virtual IControl GetTemplateFocusTarget() - { - return this; - } + protected virtual IControl? GetTemplateFocusTarget() => this; /// protected sealed override void OnAttachedToVisualTreeCore(VisualTreeAttachmentEventArgs e) @@ -173,15 +167,10 @@ namespace Avalonia.Controls } } - if (_focusAdorner != null) + if (_focusAdorner != null && GetTemplateFocusTarget() is Visual target) { - var target = (Visual)GetTemplateFocusTarget(); - - if (target != null) - { - AdornerLayer.SetAdornedElement((Visual)_focusAdorner, target); - adornerLayer.Children.Add(_focusAdorner); - } + AdornerLayer.SetAdornedElement((Visual)_focusAdorner, target); + adornerLayer.Children.Add(_focusAdorner); } } } diff --git a/src/Avalonia.Visuals/Visual.cs b/src/Avalonia.Visuals/Visual.cs index 9327531b46..80d4195421 100644 --- a/src/Avalonia.Visuals/Visual.cs +++ b/src/Avalonia.Visuals/Visual.cs @@ -150,7 +150,7 @@ namespace Avalonia public TransformedBounds? TransformedBounds => _transformedBounds; /// - /// Gets a value indicating whether the control should be clipped to its bounds. + /// Gets or sets a value indicating whether the control should be clipped to its bounds. /// public bool ClipToBounds { @@ -191,7 +191,7 @@ namespace Avalonia } /// - /// Gets a value indicating whether this control is visible. + /// Gets or sets a value indicating whether this control is visible. /// public bool IsVisible { @@ -200,7 +200,7 @@ namespace Avalonia } /// - /// Gets the opacity of the control. + /// Gets or sets the opacity of the control. /// public double Opacity { @@ -209,7 +209,7 @@ namespace Avalonia } /// - /// Gets the opacity mask of the control. + /// Gets or sets the opacity mask of the control. /// public IBrush OpacityMask { @@ -218,7 +218,7 @@ namespace Avalonia } /// - /// Gets the render transform of the control. + /// Gets or sets the render transform of the control. /// public ITransform RenderTransform { @@ -227,7 +227,7 @@ namespace Avalonia } /// - /// Gets the transform origin of the control. + /// Gets or sets the transform origin of the control. /// public RelativePoint RenderTransformOrigin { @@ -236,7 +236,7 @@ namespace Avalonia } /// - /// Gets the Z index of the control. + /// Gets or sets the Z index of the control. /// /// /// Controls with a higher will appear in front of controls with