html - 使用 MVVM 在 WPF 中显示 HTML

标签 html wpf mvvm

我在 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/

相关文章:

c# - 如何将 IsChecked 绑定(bind)到 ViewModel 子类

c# - 如何访问控件模板中的元素?

c# - 带有附加属性的疑问

安卓 MVVM : cannot generate view binders

c# - wpf mvvm绑定(bind)时间跨度,只显示分钟:seconds?

c# - 显示用于更改 ViewModel 状态的交替图像

javascript - Div 中的换行符

html - 使用 css 和 html 调整浏览器窗口大小时,如何使 div 元素 "float"向上?

html - 获取两个垂直滚动条和水平滚动条不显示

javascript - 使用 HTML 调整 div 宽度