Kris Selbekk
Kris Selbekk

Reputation: 7624

Is it possible to hide (not remove) a PivotItem?

I have a situation where I am showing a set of PivotItems and (depending on the situation) a favorites one.

I need to be able to hide this pivot when the List containing my favorites is empty - yet it needs to show up when there is something there.

Now, I could just remove it, but what about this scenario:

  1. Go to the pivot view (no favorite-pivot, since the list is empty)
  2. Go to one of the remaining PivotItems and select an item.
  3. Select that item as a favorite in its own view
  4. Tap the back-button, and return to the pivot-view.

Now there will be no favorites-pivot, and that just isn't good enough.

I have tried to remove it with Visibility="hidden", but VS is complaining about the data context not being specified properly (it is.)

Any ideas?

Upvotes: 6

Views: 3680

Answers (2)

Berni
Berni

Reputation: 510

I suppose you would have a list of favorites in that pivot item so my approach would be to bind the visibility of the pivot item to a isEmpty property of the list.

For example, the view would be

<PivotItem
  Visibility="{Binding IsNotEmpty,
  Converter={StaticResource VisibilityConverter}}"/>

and in the viewmodel

ICollectionView ItemsSource;
...
public bool IsNotEmpty(){     
    return !ItemsSource.IsEmpty;
}

and finally, the converter

public class BooleanToVisibilityConverter : IValueConverter {

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if(value == null)
        return Visibility.Collapsed;
    var isVisible = (bool)value;
    return isVisible ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    var visiblity = (Visibility)value;
    return visiblity == Visibility.Visible;
}}

Converter obtained from Useful Converters

Upvotes: 0

Wolfgang Ziegler
Wolfgang Ziegler

Reputation: 1685

Why don't add and remove the PivotItem dynamically in your code in response to user events? The user adds a favorite -> create and add the Pivot item. The user removes his last favorite item -> remove the Pivot item.

Upvotes: 3

Related Questions