あなたの子供のを管理するShowChildItems
プロパティをあなたに持たせるには、最初にそれを にする必要があります:UserControl
Visibility
GridView
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); }
}
の内部では、次の構文を使用して をこのプロパティにUserControl
バインドします。この方法では、 が何にバインドされているかは問題ではありません。GridView
Visibility
ElementName
GridView
DataContext
<GridView Visibility="{Binding ShowChildItems, ElementName=ControlRoot, Converter={StaticResource VisibilityConverter}}" ItemsSource="{Binding ChildItems}">
これを機能させるには、名前を のルート ノードに設定する必要がありUserControl
ます (残りの属性は省略しました)。
<UserControl
x:Name="ControlRoot">
また、コンバーターを使用してbool
プロパティをバインドしましたVisibility
:
<UserControl.Resources>
<local:BoolToVisibilityConverter x:Key="VisibilityConverter" />
</UserControl.Resources>
これはそのコードです:
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();
}
}
これがあなたが求めていたものであることを願っています。あなたの質問に基づいて、私はよくわかりません。