WPF Stack Panel显示隐藏孩子

时间:2012-12-19 23:23:07

标签: c# .net wpf xaml

我有一个带有两个单选按钮的stackpanel。一个是选项A,另一个是选项B.一次只能选择一个。当我检查选项B时,我想在选项B radiobuttion下面和堆栈面板中显示几个文本框。当我再次选择选项A时,文本框不应该是可见的。我怎样才能通过使用XAML来实现这一点?

4 个答案:

答案 0 :(得分:2)

您可以绑定到RadioButton IsChecked媒体资源并使用内置的BooleanToVisibilityConverter

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication4"
       Title="MainWindow" Height="300" Width="400" Name="UI" >
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter" />
    </Window.Resources>

    <Grid>
        <StackPanel Margin="0,0,0,202">
            <RadioButton x:Name="OptionA" Content="OptionA" />
            <RadioButton x:Name="OptionB" Content="OptionB" />
        </StackPanel>
        <TextBox Visibility="{Binding ElementName=OptionB, Path=IsChecked, Converter={StaticResource BoolToVisibilityConverter}}" Text="My Textbox"  Margin="0,65,165,167"/>
    </Grid>

</Window>

仅在选中“OptionB”时显示TextBox

答案 1 :(得分:2)

仅在没有转换器的XAML中。在您的资源中定义ControlTemplate,如

 <Window.Resources>
    <ControlTemplate x:Key="RadioButtonContent">
        <Grid>
            <StackPanel Margin="0,0,0,202">
                <RadioButton x:Name="OptionA" Content="OptionA" />
                <RadioButton x:Name="OptionB" Content="OptionB" />
            </StackPanel>
            <TextBox x:Name="MyTextBox" 
                     Visibility="Visible"
                     Text="My Textbox"  Margin="0,65,165,167"/>
        </Grid>
        <ControlTemplate.Triggers>
            <DataTrigger Binding="{Binding ElementName=OptionB, Path=IsChecked}" Value="False">
                <Setter TargetName="MyTextBox" Property="Visibility" Value="Hidden"/>
            </DataTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>

并像

一样使用它
<ContentControl Template="{StaticResource RadioButtonContent}"/>
希望它有所帮助..

答案 2 :(得分:1)

尝试此代码希望能解决您的问题。

将TextBox放在堆栈面板中,并将堆栈面板的Visibility属性设置为Hidden。 并在OptionButton事件上编写代码以设置stackPanel的Visibility属性。

  

    <Grid.RowDefinitions>
        <RowDefinition Height="33*"></RowDefinition>
        <RowDefinition Height="33*"></RowDefinition>
        <RowDefinition Height="33*"></RowDefinition>
    </Grid.RowDefinitions>


    <StackPanel Margin="20" Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Column="0" Grid.Row="0">
        <RadioButton Name="rdA" Content="Option A" GroupName="txtBoxGroup" Checked="rdA_Checked"></RadioButton>
        <RadioButton Name="rdB" Content="Option B" GroupName="txtBoxGroup" Checked="rdB_Checked"></RadioButton>
    </StackPanel>

    <StackPanel Margin="20" Name="TxtBxStackPanel" Height="auto"  Orientation="Vertical" Grid.Column="0" Grid.Row="1" Visibility="Hidden">
        <Label Content="My TextBox" VerticalAlignment="Center" HorizontalAlignment="Left"></Label>
        <TextBox Name="txtValue" Height="20" Width="200" HorizontalAlignment="Left"></TextBox>
    </StackPanel>

</Grid>

代码behid -

private void rdB_Checked(object sender, RoutedEventArgs e)
{

    TxtBxStackPanel.Visibility = Visibility.Visible;
}
private void rdA_Checked(object sender, RoutedEventArgs e)
{
    TxtBxStackPanel.Visibility = Visibility.Hidden;
}

享受!!!!!!

由于

Ck Nitin(TinTin)

答案 3 :(得分:0)

只有一个事件,当您选择选项A时,TextBoxes都设置为:

Textbox.Visibility = Collapsed or Hidden

取决于您希望如何显示它们。