@ -8,7 +8,7 @@ namespace Avalonia.Controls
/// </summary>
/// </summary>
public class Viewbox : Control
public class Viewbox : Control
{
{
private Decorato r _ containerVisual ;
private ViewboxContaine r _ containerVisual ;
/// <summary>
/// <summary>
/// Defines the <see cref="Stretch"/> property.
/// Defines the <see cref="Stretch"/> property.
@ -37,9 +37,8 @@ namespace Avalonia.Controls
public Viewbox ( )
public Viewbox ( )
{
{
_ containerVisual = new Decorato r( ) ;
_ containerVisual = new ViewboxContaine r( ) ;
_ containerVisual . RenderTransformOrigin = RelativePoint . TopLeft ;
_ containerVisual . RenderTransformOrigin = RelativePoint . TopLeft ;
LogicalChildren . Add ( _ containerVisual ) ;
VisualChildren . Add ( _ containerVisual ) ;
VisualChildren . Add ( _ containerVisual ) ;
}
}
@ -88,7 +87,22 @@ namespace Avalonia.Controls
if ( change . Property = = ChildProperty )
if ( change . Property = = ChildProperty )
{
{
var ( oldChild , newChild ) = change . GetOldAndNewValue < IControl > ( ) ;
if ( oldChild is not null )
{
( ( ISetLogicalParent ) oldChild ) . SetParent ( null ) ;
LogicalChildren . Remove ( oldChild ) ;
}
_ containerVisual . Child = change . GetNewValue < IControl > ( ) ;
_ containerVisual . Child = change . GetNewValue < IControl > ( ) ;
if ( newChild is not null )
{
( ( ISetLogicalParent ) newChild ) . SetParent ( this ) ;
LogicalChildren . Add ( newChild ) ;
}
InvalidateMeasure ( ) ;
InvalidateMeasure ( ) ;
}
}
}
}
@ -129,5 +143,28 @@ namespace Avalonia.Controls
return finalSize ;
return finalSize ;
}
}
private class ViewboxContainer : Control
{
private IControl ? _ child ;
public IControl ? Child
{
get = > _ child ;
set
{
if ( _ child ! = value )
{
if ( _ child is not null )
VisualChildren . Remove ( _ child ) ;
_ child = value ;
if ( _ child is not null )
VisualChildren . Add ( _ child ) ;
}
}
}
}
}
}
}
}