Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question ListView element not respecting width style

Discussion in 'UI Toolkit' started by mattseaton22, Mar 22, 2023.

  1. mattseaton22

    mattseaton22

    Joined:
    Sep 12, 2019
    Posts:
    43
    I have a ListView with a vertical list of elements.

    Both the listView and the element template have been styled with width: 100%

    The ListView fills up it's container's width.

    The elements in the list view do not fill up the width of the list view. There are large gaps on the sides. I tried different alignment options on the element template, but nothing seems to work.

    Is there a trick here?
     

    Attached Files:

  2. mattseaton22

    mattseaton22

    Joined:
    Sep 12, 2019
    Posts:
    43
    It turns out the style.width property of the template's root element is for some reason null when setting up my element references inside the template's class. I created a class for data binding on the list elements as per the docs on listview.

    I can manually style it in code here with style.width = new StyleLength(500) and it works.

    But if I use width = Length.Percent(100) it goes back to being short, and does not use the full width.

    I don't understand why it's null, or why it doesn't use 100% width when it's not null, but pixel sizing seems to work. I don't know if it's a bug or what.

    Edit: I changed the flex settings for the container to stretch and it works as expected. Not sure why 100% width doesn't work.
     
    Last edited: Mar 27, 2023