It is necessary at the touch of a button to display another element in a state of collapse . I use binding to a Boolean variable in the ViewModel and a converter, but suddenly I thought that this is an unnecessary entity, and you can certainly use it through xaml and triggers, but I can’t dig an example. Tell me.

PS I explain, implementation purely through xaml without jerking of converters and code ViewModel interests. Is it possible to cope purely by View ?

3 answers 3

There is a ready-made converter: MSDN: BooleanToVisibilityConverter - class

Here is an example of use:

 <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" Title="MainWindow" Height="350" Width="525" Name="root"> <Window.Resources> <BooleanToVisibilityConverter x:Key="VisConverter"/> </Window.Resources> <Window.Tag> <sys:Boolean>True</sys:Boolean> </Window.Tag> <Grid > <Button Content="Show/Hile" Margin="26,34,384,238" Click="Button_Click"/> <Label Content="Test text" Visibility="{Binding Tag, ElementName=root, Converter={StaticResource VisConverter}}" /> </Grid> 
  • Yes, no, I'm doing it with a converter. I just wondered whether it is possible to do it purely through xaml without tugging View model code or converters. - Sergey

Option without converter. Use Trigger.

  <ToggleButton Content="Click Me" x:Name="toggleMe" Width="150" Height="24"/> <TextBlock Text="Test textbox" HorizontalAlignment="Left" VerticalAlignment="Top"> <TextBlock.Style> <Style TargetType="{x:Type TextBlock}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=IsChecked, ElementName=toggleMe}" Value="True"> <Setter Property="Visibility" Value="Hidden"/> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> </TextBlock> 
  • Everything is good and I know this way, but you need to view by force only through xaml - if this is even possible at all. - Sergey
  • Using the ToggleButton not an option? Corrected the answer. - MaximK
  • Fit But I'm looking for a more versatile option. If it is possible, of course. - Sergey

Option using trigger and animation.

 <Button Content="Click" HorizontalAlignment="Center" VerticalAlignment="Center"> <Button.Triggers> <EventTrigger RoutedEvent="Button.Click"> <BeginStoryboard> <Storyboard> <ObjectAnimationUsingKeyFrames Storyboard.Target="{x:Reference testTextBox}" Storyboard.TargetProperty="Visibility"> <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}"/> </ObjectAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Button.Triggers> </Button> <TextBlock Text="Test textbox" x:Name="testTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Visibility="Hidden"/>