Foreach RichEditBox editbox in tabviewitem.Content - UWP C#

Hemanth B 886 Reputation points
2021-11-16T13:58:39.737+00:00

Hi, I am new to UWP, I am experienced with Winforms. How do I get each control in a selected TabViewItem in TabView.
I am using WinUI 2.7. For it to be more clear:
For example, in Winforms, for listing all controls we do something like this:

  foreach(TabPage tab in tabControl1.TabPages)
    {
          if(tabControl1.SelectedTab == tab)
          {
               foreach(RichTextBox richtext in tabPage3.Controls.OfType<RichTextBox>())
               {
                    richtext.Undo(); //Just for example
               }
          }
    }
Now I want to do the same in a tabview control in UWP C#.
Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Roy Li - MSFT 33,931 Reputation points Microsoft Vendor
    2021-11-17T06:40:58.657+00:00

    Hello,

    Welcome to Microsoft Q&A!

    First of all, based on the code you shared, you need to add the RichEditBox to the TabViewItem.Content Property first and then you could add the TabViewItem to the tabView1. Otherwise, the RichEditBox might not appear.

    Get the RichEditBox in tabviewitem.Content

    What you need to do is just get the TabViewItem.Content Property and cast the value to a RichEditBox.

    Like this:

      private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                var items = MyTabView.TabItems;  
                MUXC.TabViewItem item = items.LastOrDefault() as MUXC.TabViewItem;  
      
                // get the RichEditBox  
                RichEditBox box = item.Content as RichEditBox;  
                box.Document.SetText(Windows.UI.Text.TextSetOptions.None, "This");  
            }  
    

    This is based on the code you shared in the comment. If you have any other problems, please feel free to contact us.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.