Saarix
Topic Author
Posts: 7
Joined: 09 Jun 2021, 07:53

Binding to ActualWidth property returns 0 at runtime

22 Jun 2021, 15:14

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?
 
steveh
Posts: 42
Joined: 07 Oct 2019, 12:50

Re: Binding to ActualWidth property returns 0 at runtime

25 Jun 2021, 02:37

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
 
Saarix
Topic Author
Posts: 7
Joined: 09 Jun 2021, 07:53

Re: Binding to ActualWidth property returns 0 at runtime

27 Jun 2021, 14:00

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.
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Binding to ActualWidth property returns 0 at runtime

28 Jun 2021, 10:49

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>
 
Saarix
Topic Author
Posts: 7
Joined: 09 Jun 2021, 07:53

Re: Binding to ActualWidth property returns 0 at runtime

29 Jun 2021, 11:13

Thank you for the alternative solution.

I will post that bug report today.
 
User avatar
sfernandez
Site Admin
Posts: 2983
Joined: 22 Dec 2011, 19:20

Re: Binding to ActualWidth property returns 0 at runtime

01 Jul 2021, 10:19

Thanks for the report #2054.

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 40 guests