我如何将边框可见性绑定到包含子对象的可见性 [英] How can i bind a border visibility to the visibility of containing children objects
问题描述
我下面有这种代码,如何将 Border 的可见性绑定到所有标签的可见性?
I have this kind of code below, how can I bind the visibility of the Border to the visibility of all the labels?
当然行数和标签数不是固定的.
Of course the number of rows 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>
推荐答案
这取决于您如何更改行和标签的数量.
It depends on how you are changing the amount of rows and labels.
我假设 MyObject
是一个 List
.在这种情况下,您可以做的只是将列表绑定到 Visibility
属性,并使用 Converter
循环检查对象是否全部不可见.
I assume that MyObject
is a List<MyObject>
. In that case what you can do is simply bind the list to the Visibility
property with a Converter
that loops through the objects checking if they are all invisible.
xmlns:converters="clr-namespace:MyConverters"
窗口:
<Window.Resources>
<converters:ObjectBorderVisibilityConverter
x:Key="MyObjectBorderVisibilityConverter"/>
</Window.Resources>
<Border BorderBrush=Black
BorderThickness="{Binding MyObject, Converter={StaticResource MyObjectBorderVisibilityConverter}">
[...]
转换器代码:
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.");
}
}
}
否则,您将不得不解释如何使行和标签的数量成为动态的,我们可以从那里开始工作.
Otherwise you are going to have to explain how you got the amount of rows and labels to be dynamic and we can work from there.
希望能帮到你
u_u
好吧,根据您的评论,您有一个字符串列表,其中包含要在每个 ListViewItem
中显示的对象的名称.我不会问你为什么这样做,我想你是有原因的.我只想说你试过键值对吗?
Well according to your comment you have a list of strings which contain the name of the object you want to display in each ListViewItem
. I'm not going to ask why you are doing it this way, I assume you have a reason. I just wanna say have you tried Key Value pairs?
我在这里要做的是将网格本身作为转换器中的参数传递,并使用 LogicalTreeHelper.
What I would do here is pass the grid itself as a parameter in the converter, and loop through its children using a LogicalTreeHelper inside the converter.
<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.");
}
}
}
我手工编写了这一切,所以有很多错误,但我希望你明白这一点.
I coded this all by hand so there's prolly a bunch of errors, but I hope you get the point.
u_u
这篇关于我如何将边框可见性绑定到包含子对象的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!