Видимость дочернего элемента управления не работает внутри родителя - PullRequest
0 голосов
/ 18 декабря 2018

У меня есть StackPanel внутри Grid, который находится внутри UserControl.Я установил видимость StackPanel на основе свойства ShowInstrumentAction, которое присутствует в ViewModel, которое установлено как DataContext.Эта операция работает просто отлично.

Но у меня есть кнопка внутри этого StackPanel (пожалуйста, см. PrimeFlush3TimesButton), которую я хочу, чтобы она была видна на основании какого-то другого условия.Но видимость на уровне детей не будет работать.

Возможно ли выполнить такое связывание?
Или я должен написать стиль для видимости на кнопке?
Я не уверен, что может быть правильным подходом.Пожалуйста помоги.

<StackPanel x:Name="InstrumentOperationsPanel" Visibility="{Binding DataContext.ShowInstrumentAction,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},Converter={StaticResource boolToVisibility}}" Orientation="Horizontal" Grid.Row="0" HorizontalAlignment="Center">
    <Button x:Name="PrimeButton"
            Content="{StaticResource InstrumentPrime}"
            Margin="{StaticResource AllControlsMargin}" Command="{Binding DataContext.InstrumentPrimeCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}}">
    </Button>
    <Button x:Name="FlushButton"
            Content="{StaticResource InstrumentFlush}"
            Margin="{StaticResource AllControlsMargin}" Command="{Binding DataContext.InstrumentFlushCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}}">
    </Button>
    <Button x:Name="PrimeFlush3TimesButton"
            Content="{StaticResource PrimeAndFlush3Times}"
            Margin="{StaticResource AllControlsMargin}"
            Visibility="{Binding DataContext.IsBuiltInUser,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl},AncestorLevel=3},Converter={StaticResource boolToVisibility}}"
            Command="{Binding DataContext.InstrumentPrimeAndFlush3TimesCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}}">
    </Button>
</StackPanel>

1 Ответ

0 голосов
/ 18 декабря 2018

Используйте ElementBinding.Назовите пользовательский элемент управления.

    <Button x:Name="PrimeFlush3TimesButton"
        Content="{StaticResource PrimeAndFlush3Times}"
        Margin="{StaticResource AllControlsMargin}"
        Visibility="{Binding ElementName=UserControlName,Path=DataContext.IsBuiltInUser,Converter={StaticResource boolToVisibility}}"
        Command="{Binding ElementName=UserControlName,Path=DataContext.InstrumentPrimeAndFlush3TimesCommand}">
        </Button>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...