我将
HTML-Source字符串保存在HTMLReport字段中称为“Report”的sql Server表中(字段类型为NTEXT).现在我需要显示存储的HTML
进入WPF窗口.需要在此WPF窗口上解释HTML标记和内联CSS.
进入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描述了进行转换的方法.