【问题标题】:Insert text at caret from button in RichTextBox mvvm using wpf toolkit使用 wpf 工具包从 RichTextBox mvvm 中的按钮在插入符号处插入文本
【发布时间】:2017-11-10 23:37:32
【问题描述】:

我从 ListBox 中选择了要插入到插入符号位置的 RichTextBox 中的文本。我可以将选定的文本插入到文本字符串的末尾。

我不确定如何将 RichTextBox 插入符号位置传递给我的视图模型。

这是我的项目代码的一部分。

            <Button x:Name="AddItemBtn" Content="Add Item" HorizontalAlignment="Left" Margin="417,10,0,0" VerticalAlignment="Top" Width="100" Command="{Binding AddItemBtn}" CommandParameter="{Binding ElementName=AddItemList,Path=SelectedItem}"/>
        <wpftoolkit:RichTextBox  Grid.Column="0" Text="{Binding TestText, UpdateSourceTrigger=PropertyChanged}" x:Name="MyEditor" ScrollViewer.VerticalScrollBarVisibility="Auto" Margin="0" Height="Auto" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" Width="Auto" IsDocumentEnabled="True" AcceptsTab="True" AcceptsReturn="True" >
            <wpftoolkit:RichTextBox.Resources>
                <Style TargetType="{x:Type Paragraph}">
                    <Setter Property="Margin" Value="0" ></Setter>
                    <Setter Property="FontSize" Value="15"></Setter>
                </Style>
            </wpftoolkit:RichTextBox.Resources>
            <wpftoolkit:RichTextBox.TextFormatter>
                <wpftoolkit:PlainTextFormatter/>
            </wpftoolkit:RichTextBox.TextFormatter>
        </wpftoolkit:RichTextBox>

这是视图模型部分。

        private string _testText;
    public string TestText
    {
        get
        {
            return _testText;
        }

        set
        {
            //_testText = _testText + value;
            SetProperty(ref _testText, value);
        }

    }

    public ICommand AddItemBtn
    {
        get;
        set;
    }

    public void addItem(Tabbed selectedItem)
    {
        if (selectedItem != null)
        {
            MessageBox.Show(selectedItem.Command);
            if (TestText != null)
            {
                TestText = TestText.ToString() + selectedItem.Command;
            }
            else
            {
                TestText =  selectedItem.Command;
            }

        }
    }

我尝试了一个流文档,但仍然无法正确传递参数。

【问题讨论】:

    标签: mvvm prism richtextbox wpftoolkit


    【解决方案1】:

    我喜欢在视图模型上放置一个函数,该函数在后面的视图代码中设置。

    public class MainViewModel : ViewModelBase
    {
        public Func<int> GetCarrotPosition { get; set; }
        //...
    

    看起来你可以通过获取从文档开始到当前位置的偏移量来获取文本字符串中的字符数

        public MainWindow()
        {
            // InitializeComponent stuff..
            var castedContext = (MainViewModel)DataContext;
            castedContext.GetCarrotPosition = () =>
            {
                // Placing the cursor at the start of the text returns a value of 2, so I subtract 2 to get the current cursor location
                return MyRichTextBox.CaretPosition.DocumentStart.GetOffsetToPosition(MyRichTextBox.CaretPosition) - 2; 
            };
            //...
    

    最后,在你的命令中调用 GetCarrotPosition() 函数

        var carrotPosition = GetCarrotPosition();
        TestText.Insert(carrotPosition, selectedItem.Command);
    

    在视图模型上创建与后面的视图代码相关联的委托是我所知道的处理 UI 元素的最性感的 MVVM 方式。

    【讨论】:

    • 除此之外,您还可以在附加行为中完成所有这些操作,并拥有一些实际(可重)可用的东西,而不是在一个特定视图后面的代码中结束您的视图模型的一半......
    猜你喜欢
    • 2016-09-28
    • 1970-01-01
    • 2022-08-15
    • 2011-01-08
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    相关资源
    最近更新 更多