Howie
Howie

Reputation: 2778

Hiding elements in XAML

I have defined a XAML page for my WP8 app that currently holds a LongListSelector with an ItemTemplate.

I'm outputting some personal info like name and age. Each is a TextBlock defined like so:

<TextBlock Text="{Binding Age, StringFormat='Age: {0}'}" Visibility="{Binding AgeVisibility}"/>

The thing is that the user doesn't always input all the data, so sometimes some attributes are missing (like age). In those cases I would like to remove the TextBlock.

With the code defined like it is (note the use of the Visibility attribute) it only hides the element, which leaves an ugly space in the form.

Is there a way to remove an element from the list, if it might be undefined/missing?

EDIT: I should note that while I do use a LongListSelector, it only actually holds a single element. This element is then binded to a pure data class with many properties:

public class Details
{
    public string Name { get; set; }
    public string Age { get; set; }
}

Upvotes: 1

Views: 338

Answers (1)

Kajzer
Kajzer

Reputation: 2385

I would recommend creating an ObservableCollection of objects you'd like to bind, and adding it to the LongListSelector like this:

longListSelector.ItemsSource = myCollection;.

Everytime you'd like to remove an element from the list, you'd just call something like myCollection.RemoveAt(0) and the list will update itself.

Upvotes: 2

Related Questions