locked
Show/Hide a table RRS feed

  • Question

  • I have a small WPF application and it contains a table
    <table Name="myTable">
    
    </table>
    I want to be able to set the table to invisible from C# code. I want to be able to say :

    myTable.Visible = false;

    However, myTable does not seem to have a visible property. I am very new to WPF and any help is greatly appreciated.


    MadSeb
    Wednesday, February 3, 2010 5:23 PM

Answers

  • You could do the following; it's a little labor intensive, but may work for you.

    In your Document, place the Table in a separate document container (such as RichTextBox) and put the document container in a BlockUIContainer in the original document.  You could then change the Visibility of the RichTextBox (you probably want to change it to Collapsed, not Hidden - see Visibility documentation for the difference).

    There are some things you need to do to really make the entire content look collapsed after setting the Visibility of the RichTextBox to Collapsed (otherwise you will end of with an extra line for every hidden Table):

    1. Set the preceding Paragraph's Margin.Bottom to 0 (or the next Paragraph's Margin.Top to 0 in the event that there is no preceding Paragraph) -

    2. Set the LineHeight of the BlockUIContainer to .01

    When you make the Table Visible again reset the Margin and LineHeight.

    Xaml example showing the Table Visible:

    <FlowDocumentReader>
        <FlowDocument>
          <Paragraph Margin="auto,auto,auto,auto">This is line 1</Paragraph>   
          <BlockUIContainer Padding="0" Margin="0" >
            <RichTextBox BorderThickness="0" Visibility="Visible">
              <FlowDocument>
                <Table BorderBrush="Black" BorderThickness="2">
                  <TableRowGroup>
                    <TableRow>
                      <TableCell>
                         <Paragraph>This is document in a Table</Paragraph>
                      </TableCell>
                    </TableRow>
                  </TableRowGroup>
                </Table>
              </FlowDocument>
            </RichTextBox>
        </BlockUIContainer>
        <Paragraph>This is line 2</Paragraph>
        <Paragraph>This is line 3</Paragraph>
      </FlowDocument>
     </FlowDocumentReader>


    Xaml example showing the Table Collapsed:

    <FlowDocumentReader>
        <FlowDocument>
          <Paragraph Margin="auto,auto,auto,0">This is line 1</Paragraph>   
          <BlockUIContainer Padding="0" Margin="0" LineHeight=".01">
            <RichTextBox BorderThickness="0" Visibility="Collapsed">
              <FlowDocument>
                <Table BorderBrush="Black" BorderThickness="2">
                  <TableRowGroup>
                    <TableRow>
                      <TableCell>
                         <Paragraph>This is document in a Table</Paragraph>
                      </TableCell>
                    </TableRow>
                  </TableRowGroup>
                </Table>
              </FlowDocument>
            </RichTextBox>
        </BlockUIContainer>
        <Paragraph>This is line 2</Paragraph>
        <Paragraph>This is line 3</Paragraph>
      </FlowDocument>
     </FlowDocumentReader>

    Wednesday, February 3, 2010 6:51 PM

All replies

  • I don't believe that Table is a WPF class.

    But in WPF you do this

    Control.Visibility = System.Windows.Visibility.Hidden;

    It's not a boolean, it's an enum value.


    If my response answers your question, please mark it as the "Answer" by clicking that button above my post.

    My blog: http://www.RyanVice.net/
    Wednesday, February 3, 2010 5:33 PM
  • Table is a ContentElement and unfortunately these elements don't support the Visibility property.  You could try to add/remove the Table instead of hiding it or use a UIElement instead of a Table to display your content depending on the specifications for the layout container (i.e. does it need to support selection, editing, etc.). 
    Wednesday, February 3, 2010 5:34 PM
  • would it be possibel to place the table in some container ( a panel ? ) and hide the panel ??????
    MadSeb
    Wednesday, February 3, 2010 6:08 PM
  • You could do the following; it's a little labor intensive, but may work for you.

    In your Document, place the Table in a separate document container (such as RichTextBox) and put the document container in a BlockUIContainer in the original document.  You could then change the Visibility of the RichTextBox (you probably want to change it to Collapsed, not Hidden - see Visibility documentation for the difference).

    There are some things you need to do to really make the entire content look collapsed after setting the Visibility of the RichTextBox to Collapsed (otherwise you will end of with an extra line for every hidden Table):

    1. Set the preceding Paragraph's Margin.Bottom to 0 (or the next Paragraph's Margin.Top to 0 in the event that there is no preceding Paragraph) -

    2. Set the LineHeight of the BlockUIContainer to .01

    When you make the Table Visible again reset the Margin and LineHeight.

    Xaml example showing the Table Visible:

    <FlowDocumentReader>
        <FlowDocument>
          <Paragraph Margin="auto,auto,auto,auto">This is line 1</Paragraph>   
          <BlockUIContainer Padding="0" Margin="0" >
            <RichTextBox BorderThickness="0" Visibility="Visible">
              <FlowDocument>
                <Table BorderBrush="Black" BorderThickness="2">
                  <TableRowGroup>
                    <TableRow>
                      <TableCell>
                         <Paragraph>This is document in a Table</Paragraph>
                      </TableCell>
                    </TableRow>
                  </TableRowGroup>
                </Table>
              </FlowDocument>
            </RichTextBox>
        </BlockUIContainer>
        <Paragraph>This is line 2</Paragraph>
        <Paragraph>This is line 3</Paragraph>
      </FlowDocument>
     </FlowDocumentReader>


    Xaml example showing the Table Collapsed:

    <FlowDocumentReader>
        <FlowDocument>
          <Paragraph Margin="auto,auto,auto,0">This is line 1</Paragraph>   
          <BlockUIContainer Padding="0" Margin="0" LineHeight=".01">
            <RichTextBox BorderThickness="0" Visibility="Collapsed">
              <FlowDocument>
                <Table BorderBrush="Black" BorderThickness="2">
                  <TableRowGroup>
                    <TableRow>
                      <TableCell>
                         <Paragraph>This is document in a Table</Paragraph>
                      </TableCell>
                    </TableRow>
                  </TableRowGroup>
                </Table>
              </FlowDocument>
            </RichTextBox>
        </BlockUIContainer>
        <Paragraph>This is line 2</Paragraph>
        <Paragraph>This is line 3</Paragraph>
      </FlowDocument>
     </FlowDocumentReader>

    Wednesday, February 3, 2010 6:51 PM