none
TabControl add mutiple item on same TabControl.Items RRS feed

  • Question

  • I try to add multiple items on TabControl.

    Like below image

    My TabControl will add Canvas then add ScrollViwer on same page

    But it will add on another page.

    But my canvas and view can't add same page

    How to fix it ?




    • Edited by CY_Chen Tuesday, July 30, 2019 9:00 AM
    Monday, July 29, 2019 8:10 AM

All replies

  • Sorry, didn't pay attention for WPF. Do not known if this suit for WPF. 

    >How to fix it ?

    You need to work with pages (not a TabControl it self) on the TabControl:

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.tabcontrol?view=netframework-4.8


    Sincerely, Highly skilled coding monkey.


    Monday, July 29, 2019 8:15 AM
  • Hi  CY_Chen,

    You need to know the TabItem2 index and use the tabDynamic.Items[index] to get the TabItem.  

    Then, get the Content and add children control.

           <TabItem Header="General">
                    <Canvas>
                        
                    </Canvas>
                </TabItem>
      private void Button_Click(object sender, RoutedEventArgs e)
            {
                TabItem tbi = (TabItem)tabDynamic.Items[1];
                Canvas can = (Canvas)tbi.Content;
                can.Children.Add(new Label() { Content = "add test" });
            }
    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 29, 2019 9:43 AM
    Moderator
  • Hi Yong Lu

    I have try your method but my result is empty 

    My xaml

    <TabControl Name="tabDynamic" HorizontalAlignment="Left" Height="730" VerticalAlignment="Top" Width="800">                    
        <TabItem Header="TabItem0">
          <Grid Background="#FFE5E5E5">
                <ScrollViewer x:Name="DesignerScrollViewer0"                                                        HorizontalScrollBarVisibility="Auto"
                        </TabItem>                    
                                    
                        <Grid Height="776" Width="857"/>
                        
    </TabControl>
    Nothing show on my tabControl





    • Edited by CY_Chen Wednesday, July 31, 2019 1:44 AM
    Tuesday, July 30, 2019 1:03 AM
  • Hi   CY_Chen,

    From your code, I found the tabItems[0].Content is Grid, why you convert it to DesignerCanvas?

    Grid gd = (Grid)(tabItems[0].Content);

    Then,you can add controls in Grid or find some controls in Grid(ex: ScrollViewer,  s:ZoomBox)

    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 30, 2019 1:50 AM
    Moderator
  • Hi Yong Lu

    You mean I need put my canvas and scrollViewer on grid

    I though grid was just for layout 

    Here is my xaml I comment all tabitem portion only left tabcontrol 

    <Window x:Class="Designer.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:s="clr-namespace:D"
            WindowStartupLocation="CenterScreen"
            Title="Test"
            FontFamily="SegoeUI"
            Height="800"
            Width="1200">
        <DockPanel Margin="0">       
            <Grid Margin="0,5,0,0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="230" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Border Margin="3,1,0,0"
                        Grid.Column="1"
                        BorderBrush="LightGray"
                        BorderThickness="1">
                    <TabControl Name="tabDynamic" HorizontalAlignment="Left" Height="730" VerticalAlignment="Top" Width="800">
    
                    </TabControl>
                </Border>





    • Edited by CY_Chen Wednesday, July 31, 2019 1:46 AM
    Tuesday, July 30, 2019 2:39 AM
  • Hi   CY_Chen,

    >> My TabControl will add Canvas then add ScrollViwer on same page

    You want to put the Canvas then add ScrollViwer on the same page(In WPF, it is TabItem ).

    So, you need to get the TabItem content and add your controls.

     <TabItem Header="TabItem0">
          <Grid Background="#FFE5E5E5">
             // 
          </Grid>
     </TabItem>
    


    >>I though grid was just for layout 

    Yes, you can use the grid to adjust your layout and adjust Grid.Row dynamically.

    Or, you can use VisualTreeHelper to find the special control in TabItem or TabControl.

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 30, 2019 6:21 AM
    Moderator
  • Hi Yong Tu

    You mean TabControl add tabItem

    Then TabItem add canvas then canvas add scrollviewer

    Or tabItem add both canvas and scrollviewer?

    If is method two how to add two content both on tabitem?

    Tuesday, July 30, 2019 9:12 AM
  • Hi Yong Tu

    You mean TabControl add tabItem

    Then TabItem add canvas then canvas add scrollviewer

    Or tabItem add both canvas and scrollviewer?

    If is method two how to add two content both on tabitem?

    Hi   CY_Chen, 

    As far as I know, The tabitem has only one Content, You can use the VisualTreeHelper to find the special controls in the Content.

    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 31, 2019 1:25 AM
    Moderator