Visibility =“Collapsed”不起作用是否需要额外的东西

时间:2010-06-10 09:36:42

标签: c# wpf textbox

我想改变我TextBox的位置,当上面的文本框不可见时,我该怎样才能实现它?我正在使用wpf c#。

我正在使用Visibility="Collapsed" 但需要占用空间。

3 个答案:

答案 0 :(得分:2)

这是一个示例,根据您的问题和评论中的线索,显示我在上一条评论中的含义。它显示了可见,隐藏和折叠如何在StackPanel中,在具有固定行高的网格中以及在具有自动行高的网格中工作。

这是非常基本的东西,但希望它可以帮助您,以及任何未来的Google搜索。

<Window x:Class="CollapsedExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Collapsed Example" SizeToContent="WidthAndHeight">

    <Window.Resources>
        <Style x:Key="rectBase" TargetType="{x:Type Rectangle}">
            <Setter Property="Width" Value="100" />
            <Setter Property="Height" Value="50" />
            <Setter Property="Margin" Value="5" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Center" />

        </Style>

        <Style x:Key="rectCollapsing" 
               BasedOn="{StaticResource rectBase}"
               TargetType="{x:Type Rectangle}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=radioVisible, 
                                               Path=IsChecked}" 
                             Value="True">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=radioHidden, 
                                               Path=IsChecked}" 
                             Value="True">
                    <Setter Property="Visibility" Value="Hidden" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=radioCollapsed, 
                                               Path=IsChecked}" 
                             Value="True">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="labelStyle"
               TargetType="{x:Type Label}">
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    </Window.Resources>

    <Grid x:Name="gridLayout" 
          HorizontalAlignment="Center" 
          VerticalAlignment="Center">

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>


        <!-- Stack Panel -->
        <Label Grid.Column="0" 
               Grid.Row="0"
               Style="{StaticResource labelStyle}"
               Content="Stack Panel" />
        <StackPanel x:Name="stackExample"
                    Grid.Column="0"
                    Grid.Row="1">
            <Rectangle Style="{StaticResource rectBase}"
                       Fill="Blue" />
            <Rectangle Style="{StaticResource rectCollapsing}"
                       Fill="Red" />
            <Rectangle Style="{StaticResource rectBase}"
                       Fill="Green" />
        </StackPanel>

        <!-- Grid with Fixed Sizes -->
        <Rectangle x:Name="rectShading" 
                   Grid.Column="1" Grid.Row="0" Grid.RowSpan="2"
                   Fill="LightGray" />

        <Label Grid.Column="1" 
               Grid.Row="0"
               Style="{StaticResource labelStyle}"
               Content="Grid (Fixed Row Size)" />
        <Grid x:Name="gridFixedRowsExample"
                    Grid.Column="1"
                    Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="60" />
                <RowDefinition Height="60" />
                <RowDefinition Height="60" />
            </Grid.RowDefinitions>
            <Rectangle Style="{StaticResource rectBase}"
                       Grid.Row="0"
                       Fill="Blue" />
            <Rectangle Style="{StaticResource rectCollapsing}"
                       Grid.Row="1"
                       Fill="Red" />
            <Rectangle Style="{StaticResource rectBase}"
                       Grid.Row="2"
                       Fill="Green" />
        </Grid>


        <!-- Grid with Auto Sizes -->
        <Label Grid.Column="2" 
               Grid.Row="0"
               Style="{StaticResource labelStyle}"
               Content="Grid (Auto Row Size)" />
        <Grid x:Name="gridAutoRowsExample"
                    Grid.Column="2"
                    Grid.Row="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Rectangle Style="{StaticResource rectBase}"
                       Grid.Row="0"
                       Fill="Blue" />
            <Rectangle Style="{StaticResource rectCollapsing}"
                       Grid.Row="1"
                       Fill="Red" />
            <Rectangle Style="{StaticResource rectBase}"
                       Grid.Row="2"
                       Fill="Green" />
        </Grid>


        <!-- Options -->
        <StackPanel x:Name="stackOptions"
                    Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="3">
            <RadioButton x:Name="radioVisibile"
                         IsChecked="True"
                         Margin="5"
                         Content="Red Rectangle is Visible" />
            <RadioButton x:Name="radioHidden"
                         Margin="5"
                         Content="Red Rectangle is Hidden" />
            <RadioButton x:Name="radioCollapsed"
                         Margin="5"
                         Content="Red Rectangle is Collapsed" />
        </StackPanel>

    </Grid>
</Window>

答案 1 :(得分:0)

尝试在Visibility="Collapsed" ....

上设置TextBox

MSDN Visibility Enumeration

答案 2 :(得分:0)

如果我正确理解您,您可以尝试将GUI组件的Visibility值设置为String值。您需要做的是将其设置为System.Windows.Visibility中的预定义常量。

我们假设您正在使用代码中名为myTextBox的文本框进行处理。你需要做的是设置:

myTextBox.Visibility = System.Windows.Visibility.Collapsed;

如果您尝试将其设置为String&#34; Collapsed&#34;它不知道如何处理这个价值。