첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="셀에서 리치 텍스트 박스 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <dxb:BarManager CreateStandardLayout="True">
        <dxb:BarManager.Items>
            <dxb:BarButtonItem x:Name="undoBarButtonItem"      Command="Undo"            Content="Undo"      />
            <dxb:BarButtonItem x:Name="redoBarButtonItem"      Command="Redo"            Content="Redo"      />
            <dxb:BarButtonItem x:Name="boldBarButtonItem"      Command="ToggleBold"      Content="Bold"      />
            <dxb:BarButtonItem x:Name="italicBarButtonItem"    Command="ToggleItalic"    Content="Italic"    />
            <dxb:BarButtonItem x:Name="underlineBarButtonItem" Command="ToggleUnderline" Content="Underline" />
        </dxb:BarManager.Items>
        <dxb:BarManager.Bars>
            <dxb:Bar UseWholeRow="True">
                <dxb:Bar.DockInfo>
                    <dxb:BarDockInfo ContainerType="Top" />
                </dxb:Bar.DockInfo>
                <dxb:Bar.ItemLinks>
                    <dxb:BarButtonItemLink BarItemName="undoBarButtonItem"      />
                    <dxb:BarButtonItemLink BarItemName="redoBarButtonItem"      />
                    <dxb:BarButtonItemLink BarItemName="boldBarButtonItem"      />
                    <dxb:BarButtonItemLink BarItemName="italicBarButtonItem"    />
                    <dxb:BarButtonItemLink BarItemName="underlineBarButtonItem" />
                </dxb:Bar.ItemLinks>
            </dxb:Bar>
        </dxb:BarManager.Bars>
        <dxg:GridControl Name="gridControl">
            <dxg:GridControl.Columns>
                <dxg:GridColumn FieldName="ID"         Width="50"  />
                <dxg:GridColumn FieldName="Trademark"  Width="100" />
                <dxg:GridColumn FieldName="Model"      Width="100" />
                <dxg:GridColumn FieldName="RtfContent" Width="450" Header="Description">
                    <dxg:GridColumn.CellTemplate>
                        <DataTemplate>
                            <local:RichTextEdit x:Name="PART_Editor"
                                Height="350" />
                        </DataTemplate>
                    </dxg:GridColumn.CellTemplate>
                    <dxg:GridColumn.DisplayTemplate>
                        <ControlTemplate>
                            <local:RichTextBoxEx
                                RtfText="{Binding DisplayText, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"
                                HorizontalScrollBarVisibility="Hidden"
                                VerticalScrollBarVisibility="Hidden" />
                        </ControlTemplate>
                    </dxg:GridColumn.DisplayTemplate>
                    <dxg:GridColumn.EditTemplate>
                        <ControlTemplate>
                            <local:RichTextBoxEx x:Name="PART_Editor"
                                RtfText="{Binding EditValue, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                HorizontalScrollBarVisibility="Auto"
                                VerticalScrollBarVisibility="Visible" />
                        </ControlTemplate>
                    </dxg:GridColumn.EditTemplate>
                </dxg:GridColumn>
            </dxg:GridControl.Columns>
            <dxg:GridControl.View>
                <dxg:TableView x:Name="tableView" />
            </dxg:GridControl.View>
        </dxg:GridControl>
    </dxb:BarManager>
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

using System.Data;
using System.Windows;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            DataSet dataSet = new DataSet();

            dataSet.ReadXml("Cars2.xml");

            this.gridControl.ItemsSource = dataSet.Tables[0];
        }

        #endregion
    }
}

 

▶ RichTextBoxEx.cs

using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;

namespace TestProject
{
    /// <summary>
    /// 리치 텍스트 박스 확장
    /// </summary>
    public class RichTextBoxEx : RichTextBox
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// RTF 텍스트 속성
        /// </summary>
        public static readonly DependencyProperty RtfTextProperty = DependencyProperty.Register
        (
            "RtfText",
            typeof(string),
            typeof(RichTextBoxEx),
            new FrameworkPropertyMetadata
            (
                string.Empty,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                OnRtfTextPropertyChanged
            )
        );

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 텍스트 설정 여부
        /// </summary>
        private bool isSettingText;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region RTF 텍스트 - RtfText

        /// <summary>
        /// RTF 텍스트
        /// </summary>
        public string RtfText
        {
            get
            {
                return GetValue(RtfTextProperty) as string;
            }
            set
            {
                SetValue(RtfTextProperty, value);
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region RTF 텍스트 속성 변경시 처리하기 - OnRtfTextPropertyChanged(dependencyObject, e)

        /// <summary>
        /// RTF 텍스트 속성 변경시 처리하기
        /// </summary>
        /// <param name="dependencyObject">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private static void OnRtfTextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            ((RichTextBoxEx)dependencyObject).LoadText();
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Instance
        //////////////////////////////////////////////////////////////////////////////// Protected

        #region 텍스트 변경시 처리하기 - OnTextChanged(e)

        /// <summary>
        /// 텍스트 변경시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnTextChanged(TextChangedEventArgs e)
        {
            Binding binding = BindingOperations.GetBinding(this, RtfTextProperty);

            if(binding != null && binding.Mode != BindingMode.OneWay)
            {
                SaveText();
            }

            base.OnTextChanged(e);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Private

        #region 텍스트 로드하기 - LoadText()

        /// <summary>
        /// 텍스트 로드하기
        /// </summary>
        private void LoadText()
        {
            if(this.isSettingText)
            {
                return;
            }

            string rtfText = RtfText;

            Document.Blocks.Clear();

            if(!string.IsNullOrEmpty(rtfText))
            {
                TextRange textRange = new TextRange(Document.ContentStart, Document.ContentEnd);

                using(MemoryStream memoryStream = new MemoryStream(Encoding.ASCII.GetBytes(rtfText)))
                {
                    textRange.Load(memoryStream, DataFormats.Rtf);
                }
            }
        }

        #endregion
        #region 텍스트 저장하기 - SaveText()

        /// <summary>
        /// 텍스트 저장하기
        /// </summary>
        private void SaveText()
        {
            TextRange textRange = new TextRange(Document.ContentStart, Document.ContentEnd);

            string textRangeText = textRange.Text;

            using(MemoryStream memoryStream = new MemoryStream(textRangeText.Length * 2))
            {
                textRange.Save(memoryStream, DataFormats.Rtf);

                string rtfText = ASCIIEncoding.Default.GetString(memoryStream.ToArray());

                SetRTFText(rtfText);
            }
        }

        #endregion
        #region RTF 텍스트 설정하기 - SetRTFText(value)

        /// <summary>
        /// RTF 텍스트 설정하기
        /// </summary>
        /// <param name="value">값</param>
        private void SetRTFText(string value)
        {
            this.isSettingText = true;

            try
            {
                RtfText = value;
            }
            finally
            {
                this.isSettingText = false;
            }
        }

        #endregion
    }
}

 

▶ RichTextEdit.cs

using System.Windows.Input;

using DevExpress.Xpf.Editors;

namespace TestProject
{
    /// <summary>
    /// 리치 텍스트 에디터
    /// </summary>
    public class RichTextEdit : TextEdit
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 키 필요시 처리하기 - NeedsKey(key, modifierKeys)

        /// <summary>
        /// 키 필요시 처리하기
        /// </summary>
        /// <param name="key">키</param>
        /// <param name="modifierKeys">수정 키</param>
        /// <returns>처리 결과</returns>
        protected override bool NeedsKey(Key key, ModifierKeys modifierKeys)
        {
            if(EditMode == EditMode.InplaceActive && key == Key.Enter && (modifierKeys & ModifierKeys.Control) == 0)
            {
                return true;
            }

            return base.NeedsKey(key, modifierKeys);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요