Coping style width from one element to another

This should be something simple to do, but I’ve spent wayyy to much time on this with no sucess.

I’m creating a pseudo table. This table that has header labels setup ahead of time in the UXML (with inline style widths specified). Rows are added to the “table” in code.

I’m trying to copy the set style width from one field, so the fields are aligned (also tried minWidth) with somthing like the following:
mLabelHome.style.width = pUIManager.mCDHeaderHome.style.width;
This is not working. If I put a debug watch on pUIManager.mCDHeaderHome.style.width, I get the following message: Could not find a member width for pUIManager.mCDHeaderHome.resolvedStyle.

So clearly this is not how I need to be setting/getting width style in code… what should I be doing?

I think its suppose to be: mLabelHome.style.width = pUIManager.mCDHeaderHome.resolvedStyle.width;

I had tried that too. Similarly, when watching the pUIManager.mCDHeaderHome.resolvedStyle, I get a message of:
Could not find a member width for pUIManager.mCDHeaderHome.resolvedStyle

I can hard code a width by doing somthing like
mLabelHome.style.width = new Length(10, LengthUnit.Percent);

I can I’m starting to think this is a bug, that I can’t get the style.width.

Could someone from Unity verify this is a bug?

Are you trying to retrieve the width before the layout is calculated? That sounds like it would be the issue.
You want to make sure that UI Toolkit had a chance to calculate the full layout before you try to retrieve the values or you’ll end up with incorrect values.
You can do that by registering to the GeometryChangedEvent for example (but remember to unregister as well, otherwise you may end up with subsequent callbacks that you don’t want/need).
Hope this helps!

Ugg, yes, I was popping up a panel and setting up everything before it had done a layout calculation! Thanks.

1 Like