locked
Insert TextBlock in StackPanel side by side RRS feed

  • Question

  • Hello.

    I am developing a wpf application in which i am required to insert "n" textblocks in stackpanel. the problem is that i want to add them side by side. i.e., when one text ends the next textblock should be added right after that and not on the next line.

    when the text wraps, the next textblock should be added at the end of the previous one. (text can be of any length).

    e.g., the working of two textblocks should be like this

    The first textblock, it contains some text, and details about text, the text can be of any length

    and here it wraps to another line. (This is another text and it should appear right after the previous one)

    Monday, July 16, 2012 1:46 PM

Answers

  • Hi Atif

     

    Below are three working examples of doing this kind of wrapping thing.

     

    1. Adding TextBlocks to a WrapPanel

    2. Adding Runs to a TextBlock manually

    3. Adding Runs to a textBlock programmatically

     

    MainWindow.xaml.cs

        <StackPanel>
            <WrapPanel Margin="10">
                <TextBlock Margin="0,0,4,0" Text="This is an example"/>
                <TextBlock Margin="0,0,4,0" Text="of what I mean"/>
                <TextBlock Margin="0,0,4,0" Text="with textboxes that line up horizontally"/>
                <TextBlock Margin="0,0,4,0" Text="then wrap to a new line when there"/>
                <TextBlock Margin="0,0,4,0" Text="is no more space"/>
                <TextBlock Margin="0,0,4,0" Text="pull the window out to"/>
                <TextBlock Margin="0,0,4,0" Text="see it wrap and change"/>
            </WrapPanel>
    
            <TextBlock Margin="10" TextWrapping="Wrap"><Run Text="This is an example"/><Run Text=" of a group of"/><Run Text=" manually coded runs"/><Run Text=" which are parts of a textblock"/><Run Text=" and this is probably closer to what you want"/></TextBlock>
    
            <TextBlock x:Name="tb1" TextWrapping="Wrap" Margin="10"/>
        </StackPanel>

      

    MainWindow.xaml.cs

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                var texts = new List<string> { "This is an ", "example ", "of doing it programmatically "};
    
                foreach (var text in texts)
                    tb1.Inlines.Add(new Run { Text = text });
    
                DataContext = this;
            }
        }

     

    Try resizing the window and your will see that the first example wraps by textblock (useful in some circumstances) and the second two wrap by word, which is probably what you want?

     

    Regards,
    Pete


    #PEJL



    Monday, July 16, 2012 2:18 PM

All replies

  • Hi Atif

     

    Below are three working examples of doing this kind of wrapping thing.

     

    1. Adding TextBlocks to a WrapPanel

    2. Adding Runs to a TextBlock manually

    3. Adding Runs to a textBlock programmatically

     

    MainWindow.xaml.cs

        <StackPanel>
            <WrapPanel Margin="10">
                <TextBlock Margin="0,0,4,0" Text="This is an example"/>
                <TextBlock Margin="0,0,4,0" Text="of what I mean"/>
                <TextBlock Margin="0,0,4,0" Text="with textboxes that line up horizontally"/>
                <TextBlock Margin="0,0,4,0" Text="then wrap to a new line when there"/>
                <TextBlock Margin="0,0,4,0" Text="is no more space"/>
                <TextBlock Margin="0,0,4,0" Text="pull the window out to"/>
                <TextBlock Margin="0,0,4,0" Text="see it wrap and change"/>
            </WrapPanel>
    
            <TextBlock Margin="10" TextWrapping="Wrap"><Run Text="This is an example"/><Run Text=" of a group of"/><Run Text=" manually coded runs"/><Run Text=" which are parts of a textblock"/><Run Text=" and this is probably closer to what you want"/></TextBlock>
    
            <TextBlock x:Name="tb1" TextWrapping="Wrap" Margin="10"/>
        </StackPanel>

      

    MainWindow.xaml.cs

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                var texts = new List<string> { "This is an ", "example ", "of doing it programmatically "};
    
                foreach (var text in texts)
                    tb1.Inlines.Add(new Run { Text = text });
    
                DataContext = this;
            }
        }

     

    Try resizing the window and your will see that the first example wraps by textblock (useful in some circumstances) and the second two wrap by word, which is probably what you want?

     

    Regards,
    Pete


    #PEJL



    Monday, July 16, 2012 2:18 PM
  • awesome examples, worked for me.

    thanks XAML guy and all the best

    Monday, July 16, 2012 3:12 PM