BindableRichTextBox
От: dsalodki Беларусь http://dsalodki.wix.com/resume
Дата: 08.12.22 09:48
Оценка:
у контрола есть внутри
    public sealed class BindableRichTextBox: RichTextBox
    {
        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source",
                typeof(Uri), typeof(BindableRichTextBox),
                new PropertyMetadata(OnSourceChanged));

        public Uri Source
        {
            get => GetValue(SourceProperty) as Uri;
            set => SetValue(SourceProperty, value);
        }

        private static void OnSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (obj is BindableRichTextBox rtf && rtf.Source != null)
            {
                var stream = Application.GetResourceStream(rtf.Source);
                if (stream != null)
                {
                    var range = new TextRange(rtf.Document.ContentStart, rtf.Document.ContentEnd);
                    range.Load(stream.Stream, DataFormats.Rtf);
                }
            }
        }
    }


<UserControl
    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:RTFEditor="clr-namespace:RTFEditor"
    xmlns:ui="clr-namespace:RTFEditor"
    mc:Ignorable="d"
    x:Class="RTFEditor.RTFBox"
    x:Name="RTFEditor"    
    >

            <ui:BindableRichTextBox x:Name="RichTextControl" SpellCheck.IsEnabled="True" VerticalScrollBarVisibility="Auto" AcceptsReturn="True" AcceptsTab="True" SelectionChanged="RichTextControl_SelectionChanged" TextChanged="RichTextControl_TextChanged" KeyDown="RichTextControl_KeyDown" KeyUp="RichTextControl_KeyUp" />


и использую контрол на странице
xmlns:rtf="clr-namespace:RTFEditor;assembly=RTFEditor"
...

        <Border Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2"  BorderBrush="#E3F5FF" BorderThickness="6 6 3 6" Margin="12 3 0 6">
            <rtf:RTFBox Name="Origin"/>
        </Border>
        <Border Grid.Row="5" Grid.Column="2" Grid.ColumnSpan="2"  BorderBrush="#E3F5FF" BorderThickness="3 6 6 6" Margin="0 3 12 6">
            <rtf:RTFBox Name="Translated"/>
        </Border>


вот так я пробую связать свойство с ViewModel
    public partial class EncodingPage : Page
    {
        public EncodingPage(EncodingViewModel VM)
        {
            InitializeComponent();

            //string originDataObject = string.Empty;
            Binding originBinding = new Binding("OriginText");
            originBinding.Source = VM;//originDataObject;
            // Bind the new data source to the myText TextBlock control's Text dependency property.
            Origin.SetBinding(BindableRichTextBox.SourceProperty, originBinding);

            //string translatedDataObject = string.Empty;

            Binding translatedBinding = new Binding("TranslatedText");
            translatedBinding.Source = VM;//translatedDataObject;
            // Bind the new data source to the myText TextBlock control's Text dependency property.
            Translated.SetBinding(BindableRichTextBox.SourceProperty, translatedBinding);

            DataContext = VM;
        }


подскажите пожалуйста что не так, пустое значение всегда (пустая строка или "\r")
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.