Wpf 如果集合包含,则Visiblity设置为Visible

Wpf 如果集合包含,则Visiblity设置为Visible,wpf,xaml,Wpf,Xaml,我有两个依赖属性: public static readonly DependencyProperty GroupsProperty = DependencyProperty.Register( nameof(Groups), typeof(IEnumerable<string>), typeof(MyControl), new PropertyMetadata(null, GroupsChangedCallback)); public IEnumerabl

我有两个依赖属性:

public static readonly DependencyProperty GroupsProperty = DependencyProperty.Register(
            nameof(Groups), typeof(IEnumerable<string>), typeof(MyControl), new PropertyMetadata(null, GroupsChangedCallback));

public IEnumerable<string> Groups
{
    get => (IEnumerable<string>) GetValue(GroupsProperty);
    set => SetValue(GroupsProperty, value);
}

public static readonly DependencyProperty VisibleGroupsProperty = DependencyProperty.Register(
    nameof(VisibleGroups), typeof(IEnumerable<string>), typeof(MyControl), new PropertyMetadata(null));

public IEnumerable<string> VisibleGroups
{
    get => (IEnumerable<string>)GetValue(GroupsProperty);
    set => SetValue(GroupsProperty, value);
}
要做到这一点,没有“简单的方法”。您需要编写一些代码来确定对当前项目的引用是否在
VisibleGroups
中可用

例如,您可以创建一个检查
值[1]
是否包含
值[0]
IMultiValueConverter

<Grid>
    <Grid.Visibility>
        <MultiBinding Converter="{StaticResource converter}">
            <Binding Path="." />
            <Binding Path="VisibleGroups" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyControl}}" />
        </MultiBinding>
    </Grid.Visibility>
...


</Grid>

...

实现这一点没有“简单的方法”。您需要编写一些代码来确定当前项的引用是否在
VisibleGroups
中可用。但我如何将其绑定到
Visibility
?例如,使用转换器。但我无法绑定到ConverterParameter以在ItemsControl中传递当前组。我尝试使用IMultiValueConverter,但无法正确使用。请绑定到
可视组。你试了什么?
public class Group
{
    public string Name {get;set;}
    public bool IsVisible {get;set;}
}
<Grid>
    <Grid.Visibility>
        <MultiBinding Converter="{StaticResource converter}">
            <Binding Path="." />
            <Binding Path="VisibleGroups" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyControl}}" />
        </MultiBinding>
    </Grid.Visibility>
...


</Grid>