Search code examples
wpfdatagridwpfdatagrid

Have a DataGrid Appear under a textbox


What I want is for a datagrid to appear under a textbox when the user starts. Once focus is lost from the textbox the datagrid disappears. I am having a hard time format it so does not screw up the rest of the windows formatting.

Before you ask about using a list box, I need multiple columns and the user should be able to reorder the list.


Solution

  • One more idea. Bind the visibility of the DataGrid to a property from your ViewModel. Initially you can set the Visibility to Visible.

    Next you can use the Interactivity on the textbox's LostFocus event to change the Visibility to Hidden/Collapsed.

    The following is an example

        <TextBox Text="Test">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="LostFocus">
                    <i:InvokeCommandAction Command="{Binding DataGridVisibilityCommand}" CommandParameter="Collapsed"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>
        <DataGrid Visibility="{Binding DataGridVisibility}"/>
    

    add the namespace

        xmlns:i="http://schemas.microsoft.com/expression/2010/interactions"
    

    to your window/usercontrol and add the necessary dll references.