To have a ShowChildItems property in your UserControl to manage the Visibility of your child GridView you first need to make it a DependencyProperty:
public static readonly DependencyProperty ShowChildItemsProperty =
DependencyProperty.Register("showChildItems", typeof (bool), typeof (MyUserControl), new PropertyMetadata(true));
public bool ShowChildItems
{
get { return (bool) GetValue(ShowChildItemsProperty); }
set { SetValue(ShowChildItemsProperty, value); }
}
Inside the UserControl you will bind the GridView Visibility to this property by using the ElementName syntax - this way it doesn't matter what the GridView DataContext is bound to:
<GridView Visibility="{Binding ShowChildItems, ElementName=ControlRoot, Converter={StaticResource VisibilityConverter}}" ItemsSource="{Binding ChildItems}">
For this to work you need to set the name to UserControl's root node (I've omitted the rest of the attributes):
<UserControl
x:Name="ControlRoot">
I've also used a converter to bind a bool property to Visibility:
<UserControl.Resources>
<local:BoolToVisibilityConverter x:Key="VisibilityConverter" />
</UserControl.Resources>
This is its code:
public class BoolToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (!(value is bool)) return Visibility.Collapsed;
return (bool) value ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
I hope this is what you were asking for. I'm not quite sure based on your question.