在WPF中关于dev RichEditControl的使用

在WPF中关于RichEditControl的使用,以下代码是我采用信息发布进行简单的测试。

1、新增一篇文章,文章中包含文本、图片等信息,点击保存按钮,即可发布信息。

2、在数据行上,点击查看按钮,即可看到该文章的详细信息,此时采用  txtContents.HtmlText = dt.Rows[0]["Contents"].ToString();   即可。

3、当点击查看所有文章按钮时,需采用绑定模式,如果直接采用HtmlText进行绑定,程序会报异常,此时需采用Content,并进行内容转换即可,代码如下:

     <dxre:RichEditControl x:Name="richEditPost" ActiveViewType="Simple"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ReadOnly="True" ShowBorder="False" HorizontalScrollBarVisibility="Collapsed" VerticalScrollBarVisibility="Collapsed"   Margin="0,0,20,0" Content="{Binding Contents, Mode=OneWay, Converter={StaticResource htmlToContentConverter}}"  Width="800"  Height="300"  />

在样式中,还需增加   <dxre:HtmlToContentConverter x:Key="htmlToContentConverter" />进行转换

4、以下是测试中用到的主要代码。

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:wpfTestEdit"
        xmlns:my="clr-namespace:wpfTestEdit"
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="wpfTestEdit.ArticleAdd"
        mc:Ignorable="d"
        Title="新增文章" Height="600" Width="1000" WindowStartupLocation="CenterScreen" WindowState="Maximized">
    <Window.Resources>
        <Style TargetType="{x:Type Label}" x:Key="LableLeftStyle">
            <Setter Property="HorizontalAlignment" Value="Right"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="FontSize" Value="12" />
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="450"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="80"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Label Content="标题:" Grid.Row="0" Grid.Column="0"  Style="{StaticResource LableLeftStyle}"/>
        <dxe:TextEdit x:Name="txtTitle" HorizontalAlignment="Left" VerticalAlignment="Center" Width="350" Height="24" Grid.Row="0" Grid.Column="1"/>
        <Label Content="内容:" Grid.Row="1" Grid.Column="0"  Style="{StaticResource LableLeftStyle}"/>
        <my:myRichEdit x:Name="richText" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
        <Button x:Name="btnSave" Content="保存" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Width="50" Height="24" HorizontalAlignment="Center" Click="btnSave_Click"/>
    </Grid>
</Window>

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:wpfTestEdit"
        xmlns:dxre="http://schemas.devexpress.com/winfx/2008/xaml/richedit" x:Class="wpfTestEdit.LookArticle"
        mc:Ignorable="d"
        Title="LookArticle" Height="700" Width="1000" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen" WindowState="Maximized" >
    <Window.Resources>
        <Style TargetType="{x:Type Label}" x:Key="LableLeftStyle">
            <Setter Property="HorizontalAlignment" Value="Right"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="FontSize" Value="12" />
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>
  
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="80"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Label Content="标题:" Grid.Row="0" Grid.Column="0" Style="{StaticResource LableLeftStyle}"/>
        <Label x:Name="lblTitle" Grid.Row="0" Grid.Column="1"/>
        <Label Content="内容:" Grid.Row="1" Grid.Column="0" Style="{StaticResource LableLeftStyle}"/>
        <dxre:RichEditControl x:Name="txtContents" ReadOnly="True"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Grid.Row="1" Grid.Column="1" ActiveViewType="Simple" ShowBorder="False" ></dxre:RichEditControl>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using Library.BLL;
using Library.Model;

namespace wpfTestEdit
{
    /// <summary>
    /// LookArticle.xaml 的交互逻辑
    /// </summary>
    public partial class LookArticle : Window
    {
        public LookArticle()
        {
            InitializeComponent();
        }
        DH_News_BLL BLL = new DH_News_BLL();
        public decimal ID;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            GetNews();
        }

        private void GetNews ()
        {
            DataTable dt = BLL.GetNewsByID(ID);
            if (dt!=null && dt.Rows.Count>0)
            {
                lblTitle.Content = dt.Rows[0]["Title"].ToString();
                txtContents.HtmlText = dt.Rows[0]["Contents"].ToString();               
            }
        }
    }
}

<Window   x:Class="wpfTestEdit.LookArticleAll"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:wpfTestEdit"
       xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol"
      xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
    xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
    xmlns:dxre="http://schemas.devexpress.com/winfx/2008/xaml/richedit"
    xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
        mc:Ignorable="d"
        Title="查看所有文章" Height="700" Width="1000" Loaded="Window_Loaded" WindowStartupLocation="CenterScreen" WindowState="Maximized">
    <Window.Resources>
        <dxre:RtfToContentConverter x:Key="rtfToContentConverter" />
        <dxre:HtmlToContentConverter x:Key="htmlToContentConverter" />
        <Style TargetType="{x:Type Button}" x:Key="ButtonStyle">
            <Setter Property="Foreground"  Value="Black"/>
            <Setter Property="FontFamily" Value="Times New Roman"/>
            <Setter Property="FontSize" Value="12"/>
        </Style>
        <Style TargetType="{x:Type Label}" x:Key="LableLeftStyle">
            <Setter Property="HorizontalAlignment" Value="Right"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="FontSize" Value="12" />
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>
        <Style TargetType="{x:Type Label}" x:Key="LableRightStyle">
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="FontSize" Value="12" />
            <Setter Property="Margin" Value="3,6,0,6"/>
        </Style>
        <Style TargetType="{x:Type Label}" x:Key="LableBigStyle">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="FontSize" Value="14" />
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>
        <Style TargetType="{x:Type TextBlock}" x:Key="txtBStyle">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="FontSize" Value="12" />
            <Setter Property="LineHeight" Value="26"/>
            <Setter Property="TextWrapping" Value="Wrap"></Setter>
            <Setter Property="MaxWidth" Value="800"></Setter>
        </Style>
        <Style TargetType="{x:Type TextBlock}" x:Key="txtBformStyle">
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>

            <Setter Property="TextWrapping" Value="Wrap"></Setter>

        </Style>
        <Style TargetType="{x:Type TextBlock}" x:Key="txtTitleStyle">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="FontSize" Value="14" />
            <Setter Property="LineHeight" Value="26"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="TextWrapping" Value="Wrap"></Setter>
            <Setter Property="MaxWidth" Value="800"></Setter>
        </Style>
        <Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions >
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.15*"/>
            <ColumnDefinition Width="0.7*"/>
            <ColumnDefinition Width="0.15*"/>
        </Grid.ColumnDefinitions>

        <dxlc:GroupBox Header="查看所有文章" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Grid.Row="0" Grid.Column="1">
            <ScrollViewer  Grid.Row="0"  Grid.Column="0" VerticalScrollBarVisibility="Auto">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <ListView x:Name="listViewNews" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding  newsList}" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemContainerStyle="{StaticResource ListViewItemStyle}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="30"/>
                                        <RowDefinition Height="30"/>
                                        <RowDefinition Height="*"/>
                                 
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="0.2*"></ColumnDefinition>
                                        <ColumnDefinition Width="0.6*"></ColumnDefinition>
                                        <ColumnDefinition Width="0.15*"></ColumnDefinition>
                                        <ColumnDefinition Width="0.05*"></ColumnDefinition>
                                    </Grid.ColumnDefinitions>
                                    <StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4">
                                        <WrapPanel>
                                            <TextBlock Text="{Binding Title}" Style="{StaticResource txtTitleStyle}"/>
                                        </WrapPanel>
                                    </StackPanel>
                                    <StackPanel Grid.Row="1" Grid.Column="0">
                                        <WrapPanel>

                                            <TextBlock Text="  发表于:" Style="{StaticResource txtBStyle}"/>
                                            <TextBlock Text="{Binding AddDate}" Style="{StaticResource txtBStyle}"/>
                                        </WrapPanel>
                                    </StackPanel>

                                    <StackPanel Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="4">
                                        <WrapPanel>
                                            <TextBlock Text="内容:"  Style="{StaticResource txtBStyle}" />
                           

                                            <dxre:RichEditControl x:Name="richEditPost" ActiveViewType="Simple"  HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ReadOnly="True" ShowBorder="False" HorizontalScrollBarVisibility="Collapsed" VerticalScrollBarVisibility="Collapsed"   Margin="0,0,20,0" Content="{Binding Contents, Mode=OneWay, Converter={StaticResource htmlToContentConverter}}"  Width="800"  Height="300"  />

                                        </WrapPanel>
                                    </StackPanel>
                                </Grid>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Grid>
            </ScrollViewer>
        </dxlc:GroupBox>
   

    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using Library.BLL;
using Library.Model;
namespace wpfTestEdit
{
    /// <summary>
    /// LookArticleAll.xaml 的交互逻辑
    /// </summary>
    public partial class LookArticleAll : Window
    {
        public LookArticleAll()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {           
            GetNewsList();
        }

        DH_News_BLL BLL = new DH_News_BLL();
        private void GetNewsList()
        {
            List<DH_News> newsList = new List<DH_News>();
            DataTable dt = BLL.GetNewsList();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DH_News news = new DH_News();
                    news.ID = decimal.Parse(dt.Rows[i]["ID"].ToString());
                    news.Title = dt.Rows[i]["Title"].ToString();
                    news.Contents = dt.Rows[i]["Contents"].ToString();
                    news.AddDate = DateTime.Parse(dt.Rows[i]["AddDate"].ToString());
                    newsList.Add(news);
                }
                this.listViewNews.ItemsSource = newsList;
            }
            else
            {
                this.listViewNews.ItemsSource = null;
            }

        }
    }
}

 


猜你喜欢

转载自blog.csdn.net/xsfqh/article/details/54376052