Visiblity устанавливается на Visible, если коллекция содержит - PullRequest
1 голос
/ 27 мая 2019

У меня есть 2 свойства зависимости:

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);
}

Я связываю ItemsControl с группами, и в DataTemplate мне бы хотелось установить Visibility на Collapsed, если в VisibleGroups текущей группы нет.

<ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:MyControl}}, Path=Groups}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <DockPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
           <!-- here -->
            <Grid  Visibility="{Visible if VisibleGroups contains current group}">

            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Есть ли простой способ сделать это, или я должен создать отдельную «Групповую» модель представления, такую ​​как:

public class Group
{
    public string Name {get;set;}
    public bool IsVisible {get;set;}
}

1 Ответ

1 голос
/ 27 мая 2019

Нет «легкого пути» для достижения этой цели. Вам нужно написать некоторый код, который определяет, доступна ли ссылка на текущий элемент в VisibleGroups.

Например, вы можете создать IMultiValueConverter, который проверяет, содержит ли values[1] values[0]:

<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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...