tags:

views:

104

answers:

2

hello all

i want to change the position of my TextBox when the upper textbox is to be invisbile how can i achieve it i m using wpf c#.

i m using Visibility="Collapsed" but it take the space .

thnks in advance

shashank tyagi

A: 

Try setting up the Visibility="Collapsed" on the TextBox....

ref. http://msdn.microsoft.com/en-us/library/bb980164(v=VS.95).aspx

Cheers, G.

Gianluca Colucci
i used it but it doesn't work
Chunmun Tyagi
WPF Visibility enumerations has three values : Visible, Hidden and Collapsed. I believe the link you posted is not correct.
Markust
The link above is for Silverlight, which is slightly different. The link that I believe you want is:http://msdn.microsoft.com/en-us/library/system.windows.visibility.aspx
Wonko the Sane
Its Work Only With in A Stackpanel Not iN a grid
Chunmun Tyagi
It depends on how you define your grid. If you define your RowDefinitions to have a set height (either an actual number, or to "*"), then the row will have that height, regardless of the contents. If you set the height to Auto, it will size it to the contents.
Wonko the Sane
I confirm, the link I posted is for Silverlight... I quickly googled it just to provide a link to the user... my intention was to point him towards the right direction...As @Wonko said, I guess there are other layout issues... Collapsed should ...collapse the item and "remove" the space that it occupies. If this is not happening, it means you set up the size to some fixed value somehow... Some code to look at would be great. We might provide a better hand...
Gianluca Colucci
+1  A: 

Here is an example that shows what I mean in my last comment, based on clues from your question and comment. It shows how Visible, Hidden, and Collapsed work in a StackPanel, in a grid with fixed row heights, and in a grid with auto row heights.

This is pretty basic stuff, but hopefully it will help you, and any future Google searches.

<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>
Wonko the Sane