Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Auto for Width and Height properties #1601

Closed
wieslawsoltes opened this issue May 19, 2018 · 16 comments
Closed

Add support for Auto for Width and Height properties #1601

wieslawsoltes opened this issue May 19, 2018 · 16 comments

Comments

@wieslawsoltes
Copy link
Contributor

Repro:

<DockPanel LastChildFill="True" Background="Pink">
    <DockPanel LastChildFill="True" Width="180" DockPanel.Dock="Left">
        <Border Background="Red" Width="180" Height="250" DockPanel.Dock="Top"/>
        <Border Background="Blue" Width="180" Height="Auto" DockPanel.Dock="Bottom"/>
    </DockPanel>
    <DockPanel LastChildFill="True" Width="240" DockPanel.Dock="Right">
        <Border Background="Green" Width="240" Height="250" DockPanel.Dock="Top"/>
        <Border Background="Orange" Width="240" Height="Auto" DockPanel.Dock="Bottom"/>
    </DockPanel>
    <Grid>
        <Border Background="Gray" Width="300" Height="300"/>
    </Grid>
</DockPanel>

Exception:

Portable.Xaml.XamlObjectWriterException
  HResult=0x80131500
  Message=Could not convert object 'Auto' (of type System.String) to {http://schemas.microsoft.com/winfx/2006/xaml}Double: Auto is not a valid value for Double.
  Source=Avalonia.Markup.Xaml
  StackTrace:
   at Portable.Xaml.XamlObjectWriterInternal.GetCorrectlyTypedValue(XamlMember xm, XamlType xt, Object value)
   at Portable.Xaml.XamlObjectWriterInternal.StoreAppropriatelyTypedValue(ObjectState state, MemberAndValue ms, Object obj, Object keyObj)
   at Portable.Xaml.XamlObjectWriterInternal.OnWriteValue(Object value)
   at Portable.Xaml.XamlServices.Transform(XamlReader xamlReader, XamlWriter xamlWriter, Boolean closeWriter)
   at Avalonia.Markup.Xaml.AvaloniaXamlLoader.LoadFromReader(XamlReader reader, AvaloniaXamlContext context, IAmbientProvider parentAmbientProvider)
   at Avalonia.Markup.Xaml.AvaloniaXamlLoader.Load(Stream stream, Assembly localAssembly, Object rootInstance, Uri uri)
   at Avalonia.Markup.Xaml.AvaloniaXamlLoader.Load(Type type, Object rootInstance)
   at Avalonia.Markup.Xaml.AvaloniaXamlLoader.Load(Object obj)
   at Core2D.Avalonia.Views.MainControl.InitializeComponent() in D:\DOWNLOADS\GitHub-Avalonia\Core2D\src\Core2D.Avalonia\Views\MainControl.xaml.cs:line 26
   at Core2D.Avalonia.Views.MainControl..ctor() in D:\DOWNLOADS\GitHub-Avalonia\Core2D\src\Core2D.Avalonia\Views\MainControl.xaml.cs:line 18

Inner Exception 1:
Exception: Auto is not a valid value for Double.

Inner Exception 2:
FormatException: Input string was not in a correct format.
@wieslawsoltes
Copy link
Contributor Author

In WPF:

In addition to acceptable Double values, this property can also be Double.NaN. This is how you specify auto sizing behavior in code. In XAML you set the value to the string "Auto" (case insensitive) to enable the auto sizing behavior. Auto sizing behavior implies that the element will fill the height available to it. Note however that specific controls frequently supply default values through their default theme styles that will disable the auto sizing behavior unless it is specifically re-enabled.

https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.height%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

@grokys
Copy link
Member

grokys commented May 19, 2018

Wow, I didn't even know this was supported in WPF. Yes we should do this too.

@wieslawsoltes
Copy link
Contributor Author

wieslawsoltes commented May 19, 2018

That would be great, I came across this when doing docking control, without Auto (from code you have to set double.NaN) its impossible to position correctly the controls automatically using DockPanel for my docking control.

@wieslawsoltes
Copy link
Contributor Author

@grokys Not sure how currently double.NaN is handled for Width and Height in Avalonia across different layouts. The double.NaN seems to be default value:

/// <summary>
/// Defines the <see cref="Width"/> property.
/// </summary>
public static readonly StyledProperty<double> WidthProperty =
AvaloniaProperty.Register<Layoutable, double>(nameof(Width), double.NaN);
/// <summary>
/// Defines the <see cref="Height"/> property.
/// </summary>
public static readonly StyledProperty<double> HeightProperty =
AvaloniaProperty.Register<Layoutable, double>(nameof(Height), double.NaN);

nan
Source: WPF 4.5 Unleashed

So actually Auto is just synonym got NaN when used in Xaml.

@wieslawsoltes
Copy link
Contributor Author

@grokys Looks like I don't need Auto to get functionality as this works fine (using NaN instead of Auto):

<DockPanel LastChildFill="True" Background="Pink">
    <DockPanel LastChildFill="True" Width="180" DockPanel.Dock="Left">
        <Border Background="Red" Width="180" Height="250" DockPanel.Dock="Top"/>
        <Border Background="Blue" Width="180" Height="NaN" DockPanel.Dock="Bottom"/>
    </DockPanel>
    <DockPanel LastChildFill="True" Width="240" DockPanel.Dock="Right">
        <Border Background="Green" Width="240" Height="250" DockPanel.Dock="Top"/>
        <Border Background="Orange" Width="240" Height="NaN" DockPanel.Dock="Bottom"/>
    </DockPanel>
    <Grid>
        <Border Background="Gray" Width="300" Height="300"/>
    </Grid>
</DockPanel>

dotnet_2018-05-20_21-39-46

Anyway in the feature implementing support for Auto would be nice.

@Gillibald
Copy link
Contributor

This would involve adding a DoubleConverter the rest is just straight forward.

@Banyc
Copy link

Banyc commented Apr 2, 2020

And also something like Width="*"

@derekantrican
Copy link
Contributor

Is there a workaround for this that already exists?

@maxkatz6
Copy link
Member

@derekantrican

Is there a workaround for this that already exists?

Width="NaN"

@derekantrican
Copy link
Contributor

That doesn't seem to work (maybe I'm doing something wrong). I would imagine that my ComboBox should fill the remaining space of the column here, right?

XAML:

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto"/>
      <RowDefinition Height="*"/>
      <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="Auto"/>
      <ColumnDefinition Width="Auto"/>
      <ColumnDefinition Width="*"/>
      <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <StackPanel Orientation="Horizontal">
      <TextBlock Text="Calendar:" VerticalAlignment="Center" Margin="5"/>
      <ComboBox Height="25" Width="NaN" VerticalAlignment="Center" Margin="5"/>
    </StackPanel>
    ...
</Grid>

Screenshot:
image

@maxkatz6
Copy link
Member

Do you need HorizontalAlignment set to Stretch?

@derekantrican
Copy link
Contributor

I don't think that was required in the WPF world, but even so - setting HorizontalAlignment="Stretch" doesn't produce any different results for my situation here

@stesmi
Copy link

stesmi commented Nov 12, 2021

I know I'm resurrecting an old thread, but derek, what's the content of the ComboBox? Auto (or NaN) only takes up as much space as it needs, so if there is no contents in it, it will be tiny. "*" will fill.

In your example, the first ColumnDefinition sets it to Auto, and then the StackPanel only takes up as much space as it needs, along with the ComboBox (which is inside the StackPanel), if it's empty, doesn't need more than what it does.

@derekantrican
Copy link
Contributor

I ended up hardcoding the width (Width="190"), but haven't tried this with a newer version of Avalonia since I originally posted this.

@pavlovsg
Copy link

pavlovsg commented May 5, 2022

I ended up hardcoding the width (Width="190"), but haven't tried this with a newer version of Avalonia since I originally posted this.

It works now with Combobox.HorizontalAlignment set to Stretch.

@jpgarza93
Copy link

jpgarza93 commented Aug 29, 2023

Height="NaN" does not work for elements. Such as TextBox, ToggleSwitch, etc. They all have predefined heights and would be nice to be able to set to auto.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants