【自定義使用者控制元件】 wpf 可繫結的RichTextBox 編輯器。

小林野夫發表於2024-04-13

使用方法:

View

<Window x:Class="IndividualQAlibrary.MVVM.Views.AddQAView"
        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:IndividualQAlibrary.MVVM.Views"
        xmlns:myb="clr-namespace:IndividualQAlibrary.Theme.Behaviors"
        xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:controls="clr-namespace:IndividualQAlibrary.Theme.Controls" 
        mc:Ignorable="d"
           WindowStartupLocation="CenterScreen"
   Style="{DynamicResource QAViewStyle}"
        Title="NewQAView" Height="800" Width="1100">  
     <!--預設繫結 ContentDoucument="{Binding DocumentContent}"-->
<controls:RichTextBoxMVVMEditorView x:Name="editor"    Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="1"  >

  </controls:RichTextBoxMVVMEditorView>

</Window>
using IndividualQAlibrary.MVVM.ViewModels;
using IndividualQAlibrary.Theme.Controls;
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;

namespace IndividualQAlibrary.MVVM.Views
{
    /// <summary>
    /// NewQAView.xaml 的互動邏輯
    /// </summary>
    public partial class AddQAView : Window
    {
        public AddQAView()
        {
            InitializeComponent();
            this.DataContext = new AddQAViewModel();
 // 將顯示xaml的按鈕隱藏
            editor.DisplayXamlButton.Visibility = Visibility.Collapsed;
            editor.CloseXamlButton.Visibility = Visibility.Collapsed;
 
        }
    }
}

ViewModel

編輯器控制元件預設繫結DocumentConten

 internal partial class AddQAViewModel : ObservableObject
 {
     public AddQAViewModel()
     {
         DocumentContent = new FlowDocument();
         editState = EditStateEnum.IsNew;
     }
// 設定編輯器繫結的文件 [ObservableProperty]
private FlowDocument? documentContent; }

相關文章