使用MVVM在WPF中显示HTML
发布时间:2020-12-14 16:37:02 所属栏目:资源 来源:网络整理
导读:我将 HTML-Source字符串保存在HTMLReport字段中称为“Report”的SQL Server表中(字段类型为NTEXT).现在我需要显示存储的HTML 进入WPF窗口.需要在此WPF窗口上解释HTML标记和内联CSS. 有人可以帮我完成这段代码吗? HTMLView.xaml Window x:Class="MyProject.H
我将
HTML-Source字符串保存在HTMLReport字段中称为“Report”的SQL Server表中(字段类型为NTEXT).现在我需要显示存储的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描述了进行转换的方法.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |