Reputation: 7624
I have a situation where I am showing a set of PivotItem
s 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:
PivotItem
s and select an item.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
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
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