Search code examples
c#wpftextblock

How to show particular textblock on button click in WPF?


i am working on an application using C# WPF and MVVM.The problem i want to show summary on button click event in textblocks and textblocks are inside ListBox as follows:

 <ListBox>
           <TextBlock
               TextWrapping="Wrap"
               Height="350"
               Text="{Binding ShowVlan}"
            </TextBlock>
          <TextBlock
               TextWrapping="Wrap"
               Height="350"
               Text="{Binding ShowRouting}">
               </TextBlock>
    </ListBox>

I have two buttons and i want that if i click 1st button then 1st textblock should be display and on second button click i want to display second textblock .It is working now but the actual problem is that i want to display to on same positions but these are displaying one after another .I am also attaching screenshot for better understanding.


Solution

  • You could simply put both TextBlock at the same Grid Cell and set their Visibility to true/False based on the selected Button :

    <TextBlock x:Name="ShowVlanTb" Visibility="Hidden"
                   TextWrapping="Wrap"
                   Height="350"
                   Text="{Binding ShowVlan}" Grid.Row=1 Grid.Colomn=2
                />
              <TextBlock x:Name="ShowRoutingTb" Visibility="Hidden"
                   TextWrapping="Wrap"
                   Height="350"
                   Text="{Binding ShowRouting}" Grid.Row=1 Grid.Colomn=2>
                   />
    

    and on the button Click event handler set the Visiblity to Visible:

    ShowVlanTb.Visibility=Visibility.Visible 
    ShowRoutingTb.Visibility=Visibility.Hidden