是否可以将richtextbox与datasource绑定。怎么样? [英] Is it possible to bind richtextbox with datasource . how?

查看:101
本文介绍了是否可以将richtextbox与datasource绑定。怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本文件中使用print priview显示表数据..

I want to show table data using print priview in text file..

推荐答案

您好



使用以下用户控件,您可以将数据源绑定到richtext框



RichTextControl.xaml



Hi

Using below user control you can bind datasource with richtext box

RichTextControl.xaml

<usercontrol x:class="Controls.RichTextControl" xmlns:x="#unknown">
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <grid>
        <richtextbox x:name="RTextBox" verticalscrollbarvisibility="Auto" />
    </grid>
</usercontrol>





RichTextControl.xaml.cs





RichTextControl.xaml.cs

public partial class RichTextControl : UserControl
    {
        public RichTextControl()
        {
            InitializeComponent();
        }

        public static readonly DependencyProperty DocumentProperty =
           DependencyProperty.Register("Document", typeof(FlowDocument),
           typeof(RichTextControl), new PropertyMetadata(OnDocumentChanged));

        public FlowDocument Document
        {
            get { return (FlowDocument)GetValue(DocumentProperty); }
            set { SetValue(DocumentProperty, value); }
        }
        private bool _IsReadOnly = false;
        public bool IsReadOnly
        {
            get
            {
                return RTextBox.IsReadOnly;
            }
            set 
            {
                _IsReadOnly = value;
                RTextBox.IsReadOnly = _IsReadOnly;
            }
        }

        private static void OnDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var thisControl = (RichTextControl)d;
            thisControl.RTextBox.Document = (e.NewValue == null) ? new FlowDocument() : (FlowDocument)e.NewValue;
        }

        public void UpdateDocumentBindings()
        {
            SetValue(DocumentProperty, this.RTextBox.Document);
        }
    }





RichTextConverter.cs





RichTextConverter.cs

public class RichTextConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var flowDocument = new FlowDocument();
            if (value != null)
            {
                var xamlText = (string)value;
                flowDocument = (FlowDocument)XamlReader.Parse(xamlText);
            }
            return flowDocument;
        }
        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return string.Empty;
            var flowDocument = (FlowDocument)value;
            return XamlWriter.Save(flowDocument);
        }
    }





使用以下设置绑定





Use below to set binding

<control:richtextcontrol x:name="RTMail" document="{Binding Path=Message, Converter={StaticResource RichTextConverter}, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True,NotifyOnValidationError=True}" xmlns:x="#unknown" xmlns:control="#unknown"></control:richtextcontrol>


这篇关于是否可以将richtextbox与datasource绑定。怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆