简体   繁体   中英

Binding data in TextBlock using Run endlessly like a paragraph in WP8

I have the following XML file which used to bind the data in TextBlock.

<Guest>
  <list>
     <line index="1" name="Red Riding Hood."/>
     <line index="2" name="commonly known as Red is one of the main characters of Once
                            on a Time."/>
     <line index="3" name="Once a young."/>
     <line index="4" name="free-spirited girl who lived in a small village in the 
                            fairytale land."/>
     <line index="5" name="along with her grandmother."/>
     <line index="6" name="Red was unknowingly plagued by a curse that transformed 
                            her into a wolf with every full moon."/>
     <line index="7" name="She discovered who she really is. "/>
 </list>
</Guest>

And Here is the XAML;

<UserControl x:Class="OURBOOK.View.StoryDetail"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
 xmlns:toolkit="clrnamespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"             
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
xmlns:eim="clr_namespace:Microsoft.Expression.Interactivity.Media;assembly=Microsoft.Expression.Interactions"
xmlns:cv="clr-namespace:OURBOOK.Converter"
xmlns:ts="clr-namespace:OURBOOK.TemplateSelector"
mc:Ignorable="d"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}">

<UserControl.Resources>
    <cv:Index2ColorBrush x:Key="Index2ColorBrush"/>
    <cv:FontsizeBigger x:Key="FontsizeBigger"/>
    <cv:VisibilityBool x:Key="VisibilityBool"/>
    <cv:Index2FontFamily x:Key="Index2FontFamily"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Margin="0,0,0,0" FlowDirection="RightToLeft" >
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="0" Background="{StaticResource PhoneAccentBrush}" >
           <TextBlock  Text="{Binding Path=Name}"  
                    TextAlignment="Center" 
                    Foreground="White" 
                    TextWrapping="Wrap" 
                    Margin="20,10,10,10"/>
     </Grid>
     <Grid Grid.Row="1" >
        <ListBox x:Name="ListParagraph"
             ItemsSource="{Binding Path=ListParas}" 
             Loaded="ListParagraph_Loaded" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ts:ParagraphTemplateSelector Content="{Binding}">
                        <ts:ParagraphTemplateSelector>
                            <DataTemplate>
                                <StackPanel Background="Transparent" Margin="10,0"  >
                                    <TextBlock  x:Name="Detail" TextWrapping="Wrap">
                                        <Run Text="{Binding Path=Index, Mode=OneWay}"/>
                                        <Run Text="{Binding Path=Name, Mode=OneWay}"/>
                                    </TextBlock>
                                </StackPanel>
                            </DataTemplate>
                   </ts:ParagraphTemplateSelector>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>

So the out come of the content is displayed like following;

1 Red Riding Hood.
2 commonly known as Red is one of the main 
characters of Once on a Time3Once a young.
4 free-spirited girl who lived in a small 
village in the fairytale land.
5 along with her grandmother.
6 Red was unknowingly plagued by a curse    
that transformed her into a wolf with 
every full moon.
7 She discovered who she really is.

But I want the data to arrange like the following one by one like a paragraph; I KNOW IT IS BIT STRANGE WAY TO DO.

1 Red Riding Hood. 2 commonly known as Red is one of the main characters of Once on a Time. 3 Once a young. 4 free- spirited girl who lived in a small village in the fairytale land. 5 along with her grandmother. 6 Red was unknowingly plagued by a curse that transformed her into a wolf with every full moon. 7 She discovered who she really is.

I will appreciate if any one have a solution for this. Thanks

Just do like this,

   <StackPanel  Orientation="Horizontal">
        <TextBlock VerticalAlignment="Bottom">
           <Run Text="{Binding Path=Index, Mode=OneWay}"/>
           <Run Text="{Binding Path=Name, Mode=OneWay}"/>
        </TextBlock>
    </StackPanel>

If you want to change how items are stacked in a ListBox then you need to change ItemsPanel to horizontal WrapPanel

<ListBox x:Name="ListParagraph" ... ScrollViewer.HorizontalScrollBarVisibility="Disabled"> 
   <ListBox.ItemTemplate>
      <DataTemplate>
         <!-- your template -->
      </DataTemplate>
   </ListBox.ItemTemplate>
   <ListBox.ItemsPanel>
      <ItemsPanelTemplate>
         <WrapPanel Orientation="Horizontal"/>
      </ItemsPanelTemplate>
   </ListBox.ItemsPanel>
</ListBox>

But this will not break item in half as each item must still take one rectangle. It will only put next item in new line when it will not fit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM