How do I make a textbox visible and hidden with a checkbox?

前端 未结 3 901
甜味超标
甜味超标 2020-12-22 03:51

So I am trying to make my textbox invisible when a checkbox is not checked. Everything works fine untill I check the box and then uncheck it again. The textbox will stay vis

相关标签:
3条回答
  • 2020-12-22 03:54

    The event Checked does not fire when uncheck happens. The event Unchecked is for that purpose.

    ... Checked="chbon_Checked" Unchecked="chbon_Unchecked"/>
    

    and no need to monitor cchbon.IsChecked in code behind:

    private void chbon_Checked(object sender, RoutedEventArgs e)
    {
            txtshow.Visibility = System.Windows.Visibility.Visible;
    }
    private void chbon_Unchecked(object sender, RoutedEventArgs e)
    {
            txtshow.Visibility = System.Windows.Visibility.Hidden;
    }
    

    Alternatively, you can do it via binding and a converter:

    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVis"/>
    </Window.Resources>
    
    ...
    
    <CheckBox x:Name="chbon"/>
    <TextBox x:Name="txtshow" Visibility="{Binding ElementName=chbon, Path=IsChecked, 
             Converter={StaticResource BoolToVis}, FallbackValue=Hidden}"/>
    

    Note that, Once you managed this approach you may want to implement a custom converter since the built-in BooleanToVisibilityConverter returns Visible/Collapsed for True/False input (and not Visible/Hidden)

    0 讨论(0)
  • 2020-12-22 04:10

    when CheckBox goes to unchecked state, Unchecked event fires (simmetric to Checked). Add event handler to both of them.

    <CheckBox x:Name="chbon" 
        Content="On" 
        HorizontalAlignment="Left" 
        Margin="175,84,0,0" 
        VerticalAlignment="Top" 
        Checked="chbon_Checked_1"
        Unhecked="chbon_Checked_1"/>
    
    private void chbon_Checked_1(object sender, RoutedEventArgs e)
    {
         txtshow.Visibility = cchbon.IsChecked ? Visibility.Visible : Visibility.Hidden;
    }
    

    It is common to use binding to boolean property to set Visibility of some element. There is a BooleanToVisibilityConverter in .NET, which returns Visible for true, and Collapsed for false. Collapsed is different from Hidden: Hidden element stil claims the space on the screen as if it was Visible.

    There is a way to achieve everything in XAML using a Trigger:

    <CheckBox x:Name="chbon" Content="On" 
              HorizontalAlignment="Left" Margin="175,84,0,0" VerticalAlignment="Top"/>
    
    <TextBox x:Name="txtshow" 
             HorizontalAlignment="Left" VerticalAlignment="Top"
             Height="23" Width="29" Margin="272,82,0,0" TextWrapping="Wrap">    
        <TextBox.Style>        
            <Style TargetType="TextBox">            
                <Setter Property="Visibility" Value="Hidden"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=chbon}" Value="True">
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>            
            </Style>        
        </TextBox.Style>   
    <TextBox/>
    
    0 讨论(0)
  • 2020-12-22 04:11

    The Checked event handler will only hit when you check the checkbox, not uncheck it. You can also use the Unchecked handler in your XAML that would make the textbox hidden.

    private void chbon_Unchecked(object sender, RoutedEventArgs e)
    {
         txtshow.Visibility = System.Windows.Visibility.Hidden;
    }
    
    private void chbon_Checked_1(object sender, RoutedEventArgs e)
    {
         txtshow.Visibility = System.Windows.Visibility.Visible;
    }
    
    0 讨论(0)
提交回复
热议问题