WPF 堆栈面板显示隐藏子项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13962905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:42:25  来源:igfitidea点击:

WPF Stack Panel show hide children

c#.netwpfxaml

提问by user1527762

I have a stackpanel with two radio buttons. One is Option A and the other Option B. Only one is selectable at a time. When I check option B, I want to display a couple of text boxes right below Option B radiobuttion and within the stack panel. And when I select Option A again the textboxes should not be visible. How can I accompalish this by just using XAML?

我有一个带有两个单选按钮的堆栈面板。一个是选项 A,另一个是选项 B。一次只能选择一个。当我选中选项 B 时,我想在选项 B 单选按钮下方和堆栈面板内显示几个文本框。当我再次选择选项 A 时,文本框不应该可见。如何仅使用 XAML 来完成此操作?

采纳答案by D J

Only in XAML without even the Converter. Define ControlTemplate in your resources like

仅在 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>

and use it like

并像使用它一样

<ContentControl Template="{StaticResource RadioButtonContent}"/>

hope it helps..

希望能帮助到你..

回答by sa_ddam213

You can bind to the RadioButtonIsCheckedproperty and use the built-in BooleanToVisibilityConverter

您可以绑定到RadioButtonIsChecked属性并使用内置的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>

This will show the TextBoxonly when "OptionB" is checked.

这将TextBox仅在选中“OptionB”时显示。

回答by Ck.Nitin

Try this code Hope will solve your proble.

试试这个代码希望能解决你的问题。

Put the TextBoxes in a stack panel and set the Visibility property of stack panel is Hidden. and write the code on OptionButton event to set the Visibility property of stackPanel.

将 TextBoxes 放在堆栈面板中,并将堆栈面板的 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>

In code 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;
}

Enjoy !!!!!!

享受 !!!!!!

Thanks

谢谢

Ck Nitin (TinTin)

CK Nitin (丁丁)

回答by JosephGarrone

Just have an event that when you select Option A, both of the TextBoxesget set to:

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

Textbox.Visibility = Collapsed or Hidden

depending on how you wish to display them.

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