2010-06-10 51 views
1

我想改變我的TextBox的位置,當上面的文本框被invisbile我怎麼能實現它?我使用wpf c#。Visibility =「摺疊」不起作用是否需要額外的東西

我使用Visibility="Collapsed" 但它佔用空間。

+0

你的意思是文本框被移動頂部,底部等...像這樣 – 2010-06-10 09:46:34

+0

請更好地解釋你的情況,以便我們不必猜測。 – Amsakanna 2010-06-10 09:52:54

+1

你是積極的,你正在使用「摺疊」,而不是「隱藏」? Visibility.Hidden將使文本框不可見,但保持其位置(保留其空間),而Visibility.Collapsed將使其不可見並放棄該空間。 如果你實際上使用Collapsed,那麼你必須有其他佈局問題 - 正如@Veer所說,請解釋(和/或發佈你的例子) – 2010-06-10 13:22:24

回答

0

試穿TextBox建立Visibility="Collapsed" ....

MSDN Visibility Enumeration

+0

我使用它,但它不起作用 – Shashank 2010-06-10 11:47:26

+0

WPF可見性枚舉有三個值:可見,隱藏和摺疊。我相信你發佈的鏈接不正確。 – Marcote 2010-06-10 12:33:32

+0

上面的鏈接適用於Silverlight,稍有不同。我相信你想要的鏈接是: http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx – 2010-06-10 13:23:27

2

下面是一個例子,顯示了我的意思,我最後的評論的基礎上,從你的問題和評論的線索。它顯示了Visible,Hidden和Collapsed在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> 
0

如果我正確理解你,你試圖將一個GUI組件的Visibility值設置爲一個String值。你必須做的是將其設置爲System.Windows.Visibility中的預定義常量。

假設您正在代碼中使用名爲myTextBox的文本框。你所要做的設置:

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

如果你嘗試將其設置爲字符串「坍塌」它將不知道做什麼用的價值做。

相關問題