我在 HTMLReport 字段(字段类型为 NTEXT)中名为“Report”的 SQL Server 表中保存了 HTML-Source 字符串。现在我需要显示存储的 HTML 进入 WPF 窗口。需要在此 WPF 窗口上解释 HTML 标记和内联 CSS。
有人可以帮我完成这段代码吗?
HTMLView.xaml
<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>
HTMLViewModel.cs
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");
}
}
}
}
最佳答案
您可能想要使用 WPF RichTextBox
而不是 Winforms 的。请注意它的 Document
property属于 FlowDocument
类型。由于您拥有 HTML,因此您需要一种将 HTML 转换为 FlowDocument
的方法。 This question and answer描述一种进行转换的方法。
关于html - 使用 MVVM 在 WPF 中显示 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7720946/