Page 1 of 1

Binding to ActualWidth property returns 0 at runtime

Posted: 22 Jun 2021, 15:14
by Saarix
Hello,

I am working on a top bar that displays currencies. It should stretch its inner part based on text width, so I used binding to ActualWidth for it to work, which is fine in WPF. But in Unity it always returns 0.
If I did it without setting grid's Width, I had issues that PanelBackground would take more width than the TextBlock itself.
<!-- Coins -->
<DockPanel>
	<Control Template="{StaticResource IconPlus}" Width="120" Height="120" DockPanel.Dock="Left" Panel.ZIndex="1" />
	<Control Template="{StaticResource IconCoin}" Width="120" Height="120" DockPanel.Dock="Right" Panel.ZIndex="1" />
	<Grid Width="{Binding Path=ActualWidth, ElementName=CoinsAmount, UpdateSourceTrigger=PropertyChanged}">
		<Control Template="{StaticResource DarkPanelBackground}" Margin="-45, 40, -60, 40" />
		<TextBlock x:Name="CoinsAmount" Text="8 547" FontSize="36" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" />
	</Grid>
</DockPanel>
Any ideas how to fix this, or re-structure layout to avoid this type of solution?

Re: Binding to ActualWidth property returns 0 at runtime

Posted: 25 Jun 2021, 02:37
by steveh
I've seen this issue before, the issue is the binding evaluates prior to the first measure pass. Because you're setting the width of the parent to the actual width of a child, it means that on the first measure pass it will constrain the child to a width of 0, so the actual width can never grow to take the space.

I'm not sure what you're trying to do, but if you want the grid to scale to the child elements that will naturally happen if set the width to auto and the alignment to anything other than stretch:
<!-- Coins -->
<DockPanel>
	<Control Template="{StaticResource IconPlus}" Width="120" Height="120" DockPanel.Dock="Left" Panel.ZIndex="1" />
	<Control Template="{StaticResource IconCoin}" Width="120" Height="120" DockPanel.Dock="Right" Panel.ZIndex="1" />
	<Grid HorizontalAlignment="Center">
		<Control Template="{StaticResource DarkPanelBackground}" Margin="-45, 40, -60, 40" />
		<TextBlock x:Name="CoinsAmount" Text="8 547" FontSize="36" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" />
	</Grid>
</DockPanel>
There's more info about layout here: https://www.noesisengine.com/docs/Gui.C ... orial.html

Re: Binding to ActualWidth property returns 0 at runtime

Posted: 27 Jun 2021, 14:00
by Saarix
Yes I know that. I will grow, but the problem is since the displayed background using Control is larger it will be set to its width.
My desired behavior is that the grid would re-size based on the label width. In other words I would love to exclude that background from layouting.
Something like in Unity you can use Layout Element with Ignore Layout = true.

Idk if something like this is possible here, so my first solution was to bind it the width of the label.
Curiously tho, even if I called UpdateLayout and then set the width by hand, it was still not working.

Re: Binding to ActualWidth property returns 0 at runtime

Posted: 28 Jun 2021, 10:49
by sfernandez
If this works fine in WPF it must be a bug in Noesis. Could you please create a ticket in our bugtracker?

In the meantime you can move the binding to a container wrapping the background only:
<!-- Coins -->
<DockPanel>
  <Control Template="{StaticResource IconPlus}" Width="120" Height="120" DockPanel.Dock="Left" Panel.ZIndex="1" />
  <Control Template="{StaticResource IconCoin}" Width="120" Height="120" DockPanel.Dock="Right" Panel.ZIndex="1" />
  <Grid>
    <Border Width="{Binding Path=ActualWidth, ElementName=CoinsAmount, UpdateSourceTrigger=PropertyChanged}">
      <Control Template="{StaticResource DarkPanelBackground}" Margin="-45, 40, -60, 40" />
    </Border>
    <TextBlock x:Name="CoinsAmount" Text="8 547" FontSize="36" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" />
  </Grid>
</DockPanel>

Re: Binding to ActualWidth property returns 0 at runtime

Posted: 29 Jun 2021, 11:13
by Saarix
Thank you for the alternative solution.

I will post that bug report today.

Re: Binding to ActualWidth property returns 0 at runtime

Posted: 01 Jul 2021, 10:19
by sfernandez
Thanks for the report #2054.