我将HTML-Source字符串保存在HTMLReport字段中称为"Report"的SQL Server表中(字段类型为NTEXT).现在我需要将存储的HTML显示到WPF窗口中.需要在此WPF窗口上解释HTML标记和内联CSS.
有人可以帮我完成这段代码吗?
<Window x:Class="MyProject.HTMLView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="HTML View" Height="454" Width="787"
>
<Grid Name="grid1">
<WindowsFormsHost>
<wf:RichTextBox x:Name="reportHTML" Text="{Binding DisplayHTML, Mode=OneWay}"/>
<!-- How do i bind dispaly HTML page here-->
</WindowsFormsHost>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
namespace MyProject
{
public class HTMLViewModel: ViewModelBase
{
public HTMLViewModel()
{
//Reading from SQL Server table
//SELECT HTMLReport FROM Report WHERE ID=123
//OK, I have HTMLString from database over here
//Now I am assigning that to DisplayHTML Property
DisplayHTML ="<table><tr><td><b>This text should be in table with bold fond </b></td></tr></table>";
}
private string _displayHTML;
public string DisplayHTML
{
get
{
return _displayHTML;
}
set
{
if (_displayHTML!= value)
{
_displayHTML= value;
OnPropertyChanged("DisplayHTML");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可能想要使用WPFRichTextBox
而不是 Winforms。请注意,其Document
属性的类型为FlowDocument
。由于您拥有 HTML,因此您需要一种将 HTML 转换为FlowDocument
. 这个问题和答案描述了一种进行转换的方法。