How can I bind border visibility to visibility of contained child objects

I have the code below, how can I bind the visibility of the border to the visibility of all the shortcuts?

Of course, the number of lines and labels is not fixed.

<Border BorderBrush=Black
        BorderThickness="1,1,1,1">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
         </Grid.RowDefinitions>

         <Label DataContext="{Binding MyObject[1]}"
                Content="{Binding MyText}"
                Visibility="{Binding IsVisible}"/>

         <Label DataContext="{Binding MyObject[2]}"
                Content="{Binding MyText}"
                Visibility="{Binding IsVisible}"/>
[...]
    </Grid>
</Border>
+1
source share
1 answer

It depends on how you change the number of lines and labels.

I guess that MyObjectis List<MyObject>. In this case, you can simply bind the list to a property Visibilitywith the help Converterthat passes through the objects, checking to see if they are all invisible.

XAML:

Namespace:

xmlns:converters="clr-namespace:MyConverters"    

Window:

<Window.Resources>
    <converters:ObjectBorderVisibilityConverter 
               x:Key="MyObjectBorderVisibilityConverter"/>
</Window.Resources>


<Border BorderBrush=Black
    BorderThickness="{Binding MyObject, Converter={StaticResource MyObjectBorderVisibilityConverter}">
[...]

Transmitter Code:

namespace MyConverters
{
    public class ObjectBorderVisibilityConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility v = Visibility.Hidden;

            List<MyObject> myObjects = value as List<MyObject>;
            foreach(Object myobject in myObjects)
            {
                   if (myobject.IsVisible)
                       v = Visibility.Visible;
            }      
            return v;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new InvalidOperationException("ObjectBorderVisibilityConvertercan only be used OneWay.");        
        }
    }
}

, , , .

,

u_u


, , ListViewItem. , , , . , " "?

, , LogicalTreeHelper .

:

<Window.Resources>
<converters:ObjectBorderVisibilityConverter 
           x:Key="MyObjectBorderVisibilityConverter"/>
</Window.Resources>


<Border BorderBrush=Black
        BorderThickness="{Binding MyObject, Converter={StaticResource MyObjectBorderVisibilityConverter}", ConverterParameter={Binding ElementName=myGrid, BindsDirectlyToSource=True>
       <Grid x:Name="myGrid">
       [...]

namespace MyConverters
{
    public class ObjectBorderVisibilityConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility v = Visibility.Hidden;
            Grid myGrid = parameter as Grid;
            List<MyObject> myObjects = value as List<MyObject>;
            foreach (var child in LogicalTreeHelper.GetChildren(myGrid))
            {
                   if(child.GetType() == typeof(System.Windows.Controls.Label)
                      if (((Label)child).Visibility = Visibility.Visible)
                           v = Visibility.Visible;
            }     
            return v;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new InvalidOperationException("ObjectBorderVisibilityConvertercan only be used OneWay.");        
        }
    }
}

, , , .

u_u

+2

All Articles