[英]Binding data in TextBlock using Run endlessly like a paragraph in WP8
我有以下XML文件,該文件用於綁定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>
這是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>
因此,內容的顯示如下所示;
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.
但是我希望數據像段落一樣一個接一個地排列; 我知道這是一種奇怪的方法。
1個紅帽。 2通常被稱為紅色是“一次一次”的主要特征之一。 3一次年輕。 4個自由生活的女孩,他們住在童話般的土地上的一個小村庄。 5和她的祖母一起。 6雷德在不知不覺中被詛咒所困擾,詛咒使她每滿月都變成狼。 7她發現了她的真實身份。
如果有人對此有解決方案,我將不勝感激。 謝謝
就這樣吧
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Bottom">
<Run Text="{Binding Path=Index, Mode=OneWay}"/>
<Run Text="{Binding Path=Name, Mode=OneWay}"/>
</TextBlock>
</StackPanel>
如果要更改項目在ListBox
ItemsPanel
堆疊方式,則需要將ItemsPanel
更改為水平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>
但這不會將項目分成兩半,因為每個項目仍必須取一個矩形。 僅在不適合時才將下一項放在換行中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.