为什么richtextbox控件确实像这样工作

时间:2012-10-25 07:40:47

标签: c# wpf xaml richtextbox flowdocument

每次保存后,richtextbox总会在flowdocument中添加一对额外的大括号,如果内容是code.sorry我是中文,英文很差

-----这是我的代码-------

    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.Navigation;
    using System.Windows.Shapes;

    namespace WpfApplication1

    {



       /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnSaveFile(object sender, RoutedEventArgs e)
        {
            var dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.Filter = "*.xaml|*.xaml";
            if (dlg.ShowDialog() == true)
            {
                using (var sr=new System.IO.StreamWriter(dlg.FileName,false,Encoding.UTF8))
                {

                    sr.Write(   System.Windows.Markup.XamlWriter.Save(richtextbox.Document));
                }
            }
        }
        private void OnOpenFile(object sender, RoutedEventArgs e)
        {
            var dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Filter = "*.xaml|*.xaml";
            if (dlg.ShowDialog() == true)
            {
                    richtextbox.Document=System.Windows.Markup.XamlReader.Load(dlg.OpenFile()) as FlowDocument;   
            }
        }
    }
     }

------从visual studio复制代码并粘贴到richtextbox中,然后保存,重新加载,它就像这样-----

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingSystem.Windows.Data;
usingSystem.Windows.Documents;
usingSystem.Windows.Input;
usingSystem.Windows.Media;
usingSystem.Windows.Media.Imaging;
usingSystem.Windows.Navigation;
usingSystem.Windows.Shapes;

namespaceWpfApplication1
{}{
    /// <summary>
    ///MainWindow.xaml 的交互逻辑
    /// </summary>
public partial class MainWindow : Window
{
    publicMainWindow()
    {
        InitializeComponent();
    }

    private voidOnSaveFile(object sender, RoutedEventArgse)
    {
        var dlg = newMicrosoft.Win32.SaveFileDialog();
        dlg.Filter = "*.xaml|*.xaml";
        if (dlg.ShowDialog() == true)
        {
            using(varsr=newSystem.IO.StreamWriter(dlg.FileName,false,Encoding.UTF8))
            {

                sr.Write(   System.Windows.Markup.XamlWriter.Save(richtextbox.Document));
            }
        }
    }
    private voidOnOpenFile(object sender, RoutedEventArgse)
    {
        var dlg = newMicrosoft.Win32.OpenFileDialog();
        dlg.Filter = "*.xaml|*.xaml";
        if (dlg.ShowDialog() == true)
        {
                richtextbox.Document=System.Windows.Markup.XamlReader.Load(dlg.OpenFile()) as FlowDocument;   


  }
    }
}

}

0 个答案:

没有答案