question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Visibility via binding is not triggered for elements inside ListView item templates

See original GitHub issue

The scenario: We have a ListView or RadListView with similar item template

<StackLayout height="80" class="m-10 p-10" backgroundColor="gray" orientation="vertical">
    <Label  text="{{ id + ' tap to change the visibility of the second label'}} " backgroundColor="white"/>
    <Label text="{{ name }}" visibility="{{ status+id ? 'visible' : 'collapsed' }}" backgroundColor="blue"//>
</StackLayout>

On itemTap, we are changing the boolean status to true. The binding is set successfully - however, the Label remains collapsed.

Test application to fully reproduce this case can be found here.

Steps to reproduce:

  • open the application - a ListView will load showing only the first Label from each item template.
  • on itemTap the visibility should change to visible, but in fact, it will remain collapsed.

Reported via t.1114737

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
NickIlievcommented, Oct 4, 2017

@vegeorgi , @sitefinitysteve

To change the visibility for items inside ListView item template we can create Observable objects with our desired properties and callbacks and bind directly to them. Doing that will assure that we are working with the proper context (when accessing this in JavaScript) in our ListView templates .

In the list view item template this is the current item so we can create an observable object with the wanted properties and callbacks and this way we won’t need to access parent binding in order to work with a callback from the global context.
With this approach, we also won’t have to worry about creating multiple unique properties for each item as we would directly access the current object.

For example:

import { Observable } from 'data/observable';

// our observable item with text property and isItemVisible callback
class Item extends Observable {

    constructor(public text: string, public isItemVisible: boolean) { 
        super();
    }

    public toggleVisibility(args) {
        // console.dir(this);

        this.set("isItemVisible", !this.isItemVisible);
        console.log("toggleVisibility value: " + this.isItemVisible);
    }
}

export class HelloWorldModel extends Observable {

    public items: Array<any>;

    constructor() {
        super();
        
        // the source array
        this.items = [
            new Item("1", true),
            new Item("2", true),
            new Item("3", true),
            new Item("4", true),
        ]
    }
}

XML file

<ListView items="{{ items }}" height="500" loaded="onLoaded" itemLoading="onItemLoading" itemTap="onItemTap">
	<ListView.itemTemplate>
		<StackLayout>
                         <!-- now we can access the callback of the current observable item as `this` is the current item (with text, isItemVisible properties and toggleVisibility callback) -->
			<Label text="{{ text }}" textWrap="true" />
			<Button text="tap" tap="{{ toggleVisibility }} " /> 
			<Label text="hide/show" visibility="{{ isItemVisible ? 'visible' : 'collapsed' }}" textWrap="true" />

		</StackLayout>
	</ListView.itemTemplate>
</ListView>

Full demonstration application can be found here.

Note: On iOS, the list view cells won’t be remeasured dynamically when the visibility is changed (by design). That means that we can only show/hide the content via visibility but without remeasuring the item cell. On Android, by design, the cells will be remeasured.

0reactions
lock[bot]commented, Dec 12, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Visibility binding to ListboxItems inside template - Stack Overflow
I'm trying to change the Visibility of elements inside a ListBoxItem by clicking on a CheckBox outside the ListBox which contains Items, but...
Read more >
Visibility via binding is not triggered for elements inside ListView ...
The scenario: We have a ListView or RadListView with similar item template <StackLayout height="80" class="m-10 p-10" backgroundColor="gray" ...
Read more >
ListView Styles and Templates - WPF .NET Framework
Review a list of topics describing the styles and templates available for use with ListView control.
Read more >
Element: <oj-list-view> - Oracle Help Center
Description: The JET ListView enhances a HTML list element into a themable, ... updating observableArray in a foreach binding), is not supported.
Read more >
ListView - Android Developers
A list view is an adapter view that does not know the details, such as type and ... In order to display items...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found