我能够在线找到将文本绑定到WinRT RichTextBlock
的唯一示例如下所示:
<RichTextBlock>
<Paragraph>
<Run Text="{Binding Content}"/>
</Paragraph>
</RichTextBlock>
我能够找到实际显示富文本的唯一示例如下:
<RichTextBlock>
<Paragraph>
<Run>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ligula nisi, vehicula nec eleifend vel, rutrum non dolor. Vestibulum ante ipsum primis in faucibus orci</Run>
<Run FontSize="30">luctus</Run>
<Run>et ultrices posuere cubilia Curae; Curabitur elementum scelerisque accumsan. In hac habitasse platea dictumst. Maecenas eu nibh vitae nibh laoreet placerat. Duis dolor ante, semper luctus ullamcorper eget, placerat et ligula. Donec placerat tincidunt vehicula. Fusce condimentum lacus quis libero blandit semper sed vel quam. Proin eget nisl lacinia nibh convallis scelerisque at sed massa. Duis commodo tincidunt consequat. Duis malesuada, nisl a pharetra placerat, odio dui suscipit quam, vitae rhoncus sem risus quis odio. Aliquam justo nunc, adipiscing id elementum sit amet, feugiat vel enim. Aliquam pharetra arcu nec elit luctus euismod. Suspendisse potenti.</Run>
</Paragraph>
</RichTextBlock>
我如何将RichTextBlock
的文本数据绑定到我的视图模型中可能包含多个段落和运行的属性?该视图模型属性需要什么类型?
我看过一些使用FlowDocument
的引用,但我不知道这是否适用于RichTextBlock
。但是,即使这些示例也没有显示任何绑定到文档的数据。
答案 0 :(得分:2)
RichTextBlock似乎不提供文档绑定。相反,您可以使用自定义RichTextBlock控件来实现它。您可以尝试Bindable RichTextBlock
答案 1 :(得分:1)
我已将数据绑定到我的一个项目中的RichTextBlock。请参阅下面的XAML。我绑定了一些字符串值和图像。您可以在此处查看页面呈现方式的快照:
<common:RichTextColumns x:Name="richTextColumns" Margin="117,0,117,47" VerticalAlignment="Top">
<RichTextBlock x:Name="richTextBlock" Width="560" Style="{StaticResource ItemRichTextStyle}" TextWrapping="Wrap">
<Paragraph>
<Run FontSize="20" FontWeight="Light" Text="{Binding Title}"/>
<LineBreak/>
</Paragraph>
<Paragraph LineStackingStrategy="MaxHeight">
<InlineUIContainer>
<ItemsControl ItemsSource="{Binding Authors}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<HyperlinkButton Content="{Binding}"
VerticalAlignment="Center" FontSize="14"
Tapped="AuthorSearchLinkTapped"
IsTapEnabled="True"
Padding="0, 0, 0, 0"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</InlineUIContainer>
</Paragraph>
<Paragraph LineStackingStrategy="MaxHeight">
<InlineUIContainer>
<Image x:Name="image" MaxHeight="200" Margin="0,20,0,10" Stretch="Uniform" Source="{Binding ImageUrl}"/>
</InlineUIContainer>
</Paragraph>
<Paragraph LineStackingStrategy="MaxHeight">
<InlineUIContainer>
<Grid Width="560" Height="125" MaxHeight="125">
<TextBlock Text="Loading ad..."
VerticalAlignment="Center"
HorizontalAlignment="Center"
Style="{StaticResource BasicTextStyle}"/>
<UI:AdControl ApplicationId="ec6615c8-dc88-4413-af37-1fc3b5603e85"
AdUnitId="104236"
Width="250" Height="125"
HorizontalAlignment="Center"/>
</Grid>
</InlineUIContainer>
</Paragraph>
<Paragraph>
<Run FontWeight="SemiLight" Text="{Binding Description}"/>
</Paragraph>
</RichTextBlock>
<!-- Additional columns are created from this template -->
<common:RichTextColumns.ColumnTemplate>
<DataTemplate>
<RichTextBlockOverflow Width="560" Margin="80,0,0,0">
<RichTextBlockOverflow.RenderTransform>
<TranslateTransform X="-1" Y="4"/>
</RichTextBlockOverflow.RenderTransform>
</RichTextBlockOverflow>
</DataTemplate>
</common:RichTextColumns.ColumnTemplate>
</common:RichTextColumns>
答案 2 :(得分:1)
我遇到了同样的问题,我的解决方案是手动完成。
RichTextBlock
这是代码。
private void Page_OnLoaded(object sender, RoutedEventArgs e)
{
SetReferences();
(DataContext as INotifyPropertyChanged).PropertyChanged += OnPropertyChanged;
}
private void Page_OnUnloaded(object sender, RoutedEventArgs e)
{
(DataContext as INotifyPropertyChanged).PropertyChanged -= OnPropertyChanged;
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
var propertyName = GetPropertyName<MyViewModel, object>(x => x.References); // I use this to avoid bugs at runtime
if (e.PropertyName == propertyName)
SetReferences();
}
private void SetReferences()
{
var references = (DataContext as MyViewModel).References;
ReferencesRichTextBlock.Blocks.Clear();
foreach (var reference in references)
{
var paragraph = new Paragraph();
paragraph.Inlines.Add(new Run { Text = reference.Title, FontWeight = FontWeights.Bold});
paragraph.Inlines.Add(new Run { Text = " : "});
paragraph.Inlines.Add(new Run { Text = reference.Content});
paragraph.Inlines.Add(new LineBreak());
ReferencesRichTextBlock.Blocks.Add(paragraph);
}
}
public static string GetPropertyName<T, P>(Expression<Func<T, P>> action) where T : class
{
var expression = (MemberExpression)action.Body;
var name = expression.Member.Name;
return name;
}
当然,这是xaml
<Page ...
Loaded="Page_OnLoaded"
Unloaded="Page_OnUnloaded">
...
<RichTextBlock x:Name="ReferencesRichTextBlock">
</RichTextBlock>
</Page>
答案 3 :(得分:0)
我有类似的情况。我想根据一些正在进行的操作更新ReadOnly RichTextBox的内容和状态消息。这就是我最终做的事情:
<UserControl
x:Class="RawhideCsControl.StorageViewUserControl2"
<!-- >
<RichTextBox x:Name="StatusText" HorizontalAlignment="Left" Height="350.72" VerticalAlignment="Top" Width="636">
<FlowDocument>
<Paragraph>
<Run Text="{Binding Status}"></Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
<!-- >
</UserControl>
代码:
public partial class StorageViewUserControl2 : System.Windows.Controls.UserControl
{
// . . .
StatusChanger statusChanger = new StatusChanger();
private void AddStatusLine(string format, params object[] args)
{
if (args.Length > 0)
format = string.Format(format, args);
//m_StorageViewTreeModel.MirrorStatusDisplay += string.Format("{0}\r\n", format);
statusChanger.Status += string.Format("{0}\r\n", format);
}//end this.AddStatusLine(str)
// . . .
} // end partial class
public class StatusChanger : INotifyPropertyChanged
{
private string status = string.Empty;
public string Status
{
get { return status; }
set
{
status = value;
OnPropertyChanged("Status");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged( string PropertyName )
{
if( PropertyChanged != null )
PropertyChanged( this, new PropertyChangedEventArgs(PropertyName));
}
} // end class StatusChanger