locked
Hide all controls in column RRS feed

  • Question

  • Hi,

    I want to hide all the controls in a column in my WPF application.

    Note - I dont want to hide column, I want to hide its contents only.

     

    Please help me.

     

    Saurabh

    Thursday, November 10, 2011 5:38 AM

Answers

  • Hi Mysterious,

    Refer to Abhinav Dawra's code snippet:

    foreach (UIElement element in grd1.Children)
    {
             int columnIndex = System.Windows.Controls.Grid.GetColumn(element);
             if (columnIndex == 0)
             {
                   element.Visibility = Visibility.Hidden;
              }
    }
    
    

    you could define a function, and pass the colum as parameter, and then check if that element is in this column.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Sheldon _Xiao Friday, November 18, 2011 7:53 AM
    Monday, November 14, 2011 8:32 AM
  • Hi Saurabh,
       What ever my understanding is that - you will have column in your application. You need to hide the controls of the column. I do not understand the what do you mean by column. So I assumed that you have a stack panel named "stackPanel1" where i have added few controls "textbox1" and "Label1".
     I have given some color to the controls to make understand that stack panel is not hiding but the controls
     of the are hiding. Please ignore the color combination (:
    <Window x:Class="Tets.HideColumncontrol"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="HideColumncontrol" Height="370" Width="365">
      <Grid>
            <StackPanel Margin="0,0,20,30" Height="152" Name="stackPanel1" Width="200" Background="#FFE7F59A">
                <TextBox Name="textbox1" Background="#FFF5B3B3"></TextBox>
                <Label Name="Label1" Background="#FF83C1F8" Height="25"></Label>
            </StackPanel>
            <Button Content="Button" Height="24" Name="button1" Width="91" Margin="132,276,120,32" Click="button1_Click" />
        </Grid>
    </Window>
    
    in the code behind i.e HideColumncontrol.xaml.cs
    private void button1_Click(object sender, RoutedEventArgs e)
            {
                foreach (UIElement contrl in stackPanel1.Children)
                {
                    contrl.Visibility =System.Windows.Visibility.Hidden;
                }
            }
    

    Pleased to answer you further if i am wrong...

    Thanks & Regards

    KashNetDev

    Please remember to click “Mark as Answer” on the post that helps you,This can be beneficial to other community members reading the thread.

    • Edited by KashNetDev Thursday, November 10, 2011 6:21 AM
    • Marked as answer by Sheldon _Xiao Friday, November 18, 2011 7:53 AM
    Thursday, November 10, 2011 6:20 AM
  • Hi Myterious

    Are you talking about Grid or DataGrid control in WPF?

    However, I somewhere that you wanted to hide all controls in a column of a WPF grid.

    Here you go with the code.

     

    foreach (UIElement element in grd1.Children)
                {
                    int columnIndex = System.Windows.Controls.Grid.GetColumn(element);
                    if (columnIndex == 0)
                    {
                        element.Visibility = Visibility.Hidden;
                    }
                }
    


    Hope it helps.

     

    Thanks,

    Abhinav

    • Proposed as answer by Sheldon _Xiao Monday, November 14, 2011 8:29 AM
    • Marked as answer by Sheldon _Xiao Friday, November 18, 2011 7:53 AM
    Thursday, November 10, 2011 9:46 AM

All replies

  • Hi,

    Probably the easiest way of hiding a control is to set the Visibility to Collapsed.

    Regards

    Thursday, November 10, 2011 6:17 AM
  • Hi Saurabh,
       What ever my understanding is that - you will have column in your application. You need to hide the controls of the column. I do not understand the what do you mean by column. So I assumed that you have a stack panel named "stackPanel1" where i have added few controls "textbox1" and "Label1".
     I have given some color to the controls to make understand that stack panel is not hiding but the controls
     of the are hiding. Please ignore the color combination (:
    <Window x:Class="Tets.HideColumncontrol"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="HideColumncontrol" Height="370" Width="365">
      <Grid>
            <StackPanel Margin="0,0,20,30" Height="152" Name="stackPanel1" Width="200" Background="#FFE7F59A">
                <TextBox Name="textbox1" Background="#FFF5B3B3"></TextBox>
                <Label Name="Label1" Background="#FF83C1F8" Height="25"></Label>
            </StackPanel>
            <Button Content="Button" Height="24" Name="button1" Width="91" Margin="132,276,120,32" Click="button1_Click" />
        </Grid>
    </Window>
    
    in the code behind i.e HideColumncontrol.xaml.cs
    private void button1_Click(object sender, RoutedEventArgs e)
            {
                foreach (UIElement contrl in stackPanel1.Children)
                {
                    contrl.Visibility =System.Windows.Visibility.Hidden;
                }
            }
    

    Pleased to answer you further if i am wrong...

    Thanks & Regards

    KashNetDev

    Please remember to click “Mark as Answer” on the post that helps you,This can be beneficial to other community members reading the thread.

    • Edited by KashNetDev Thursday, November 10, 2011 6:21 AM
    • Marked as answer by Sheldon _Xiao Friday, November 18, 2011 7:53 AM
    Thursday, November 10, 2011 6:20 AM
  • you are right but how can i get all controls of that column. because column is not fixed For example sometimes i need to hide controls for 3rd column ..sometime for 3rd and 4th. so how can i get all controls in a particular column dynamically...
    Thursday, November 10, 2011 6:21 AM
  • Hi Myterious

    Are you talking about Grid or DataGrid control in WPF?

    However, I somewhere that you wanted to hide all controls in a column of a WPF grid.

    Here you go with the code.

     

    foreach (UIElement element in grd1.Children)
                {
                    int columnIndex = System.Windows.Controls.Grid.GetColumn(element);
                    if (columnIndex == 0)
                    {
                        element.Visibility = Visibility.Hidden;
                    }
                }
    


    Hope it helps.

     

    Thanks,

    Abhinav

    • Proposed as answer by Sheldon _Xiao Monday, November 14, 2011 8:29 AM
    • Marked as answer by Sheldon _Xiao Friday, November 18, 2011 7:53 AM
    Thursday, November 10, 2011 9:46 AM
  • Hi Mysterious,

    Refer to Abhinav Dawra's code snippet:

    foreach (UIElement element in grd1.Children)
    {
             int columnIndex = System.Windows.Controls.Grid.GetColumn(element);
             if (columnIndex == 0)
             {
                   element.Visibility = Visibility.Hidden;
              }
    }
    
    

    you could define a function, and pass the colum as parameter, and then check if that element is in this column.

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Sheldon _Xiao Friday, November 18, 2011 7:53 AM
    Monday, November 14, 2011 8:32 AM