如何设置RichTextBox字体的下一个文字被写入? [英] How to set RichTextBox Font for the next text to be written?

查看:150
本文介绍了如何设置RichTextBox字体的下一个文字被写入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为下一个要写入RichTextBox的文本设置字体系列。 ...
$ b pre $ < RichTextBox x:Name =RichTextEditorMaxWidth =1000
FontFamily ={Binding ElementName = TextFontComboBox,Path = SelectedItem}
FontSize ={Binding ElementName = TextSizeComboBox,Path = SelectedValue}
Width = AutoHeight =AutoHorizo​​ntalScrollBarVisibility =Auto
VerticalScrollBarVisibility =Auto/>

...但它改变了整个文本。我想通过选择属性,我可以限制更改只适用于选定的区域。但是如何为下一个-not尚未输入文本?

解决方案

为了设置基于光标位置的FontFamily,你需要使用依赖项属性定义一个自定义控件,通过重写OnTextInput方法来帮助插入一个新的Run部分。



我包括了大部分的代码,你需要修改命名空间,以适应您的开发环境。

代码使用ViewModel来管理可用的字体,并管理字体是否改变。
这段代码只是一个原型,并不涉及两个控件之间的聚焦问题。



使用这段代码:

1 - 在RichTectBox中键入一些文本。

2-在ComboBox中更改字体。

3-返回到RichTextBox。

4-键入some更多的文字。

这是自定义的RichTextBox控件:使用System.Windows的

  ; 
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;

namespace RichTextboxFont.Views
{
public class RichTextBoxCustom:RichTextBox
{
public static readonly DependencyProperty CurrentFontFamilyProperty =
DependencyProperty.Register( CurrentFontFamily,
typeof(FontFamily),typeof
(RichTextBoxCustom),
新的FrameworkPropertyMetadata(新的FontFamily(Tahoma),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
PropertyChangedCallback (OnCurrentFontChanged)));
$ b $ public FontFamily CurrentFontFamily
{
get
{
return(FontFamily)GetValue(CurrentFontFamilyProperty);
}
set
{
SetValue(CurrentFontFamilyProperty,value);


$ b $ private void OnCurrentFontChanged(DependencyObject o,DependencyPropertyChangedEventArgs e)
{}

protected override OnTextInput(TextCompositionEventArgs e )
{
ViewModels.MainViewModel mwvm = this.DataContext as ViewModels.MainViewModel;
if((mwvm!= null)&&(mwvm.FontChanged))
{
TextPointer textPointer = this.CaretPosition.GetInsertionPosition(LogicalDirection.Forward);
Run run = new Run(e.Text,textPointer);
run.FontFamily = this.CurrentFontFamily;
this.CaretPosition = run.ElementEnd;
mwvm.FontChanged = false;
}
else
{
base.OnTextInput(e);



$ b code


$ b是XAML:

 < Window x:Class =RichTextboxFont.Views.MainView
xmlns =http ://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:local = clr-namespace:RichTextboxFont.Views
xmlns:ViewModels =clr-namespace:RichTextboxFont.ViewModels
Title =Main Window
Height =400Width =800 >
< DockPanel>
<网格>
< Grid.RowDefinitions>
< RowDefinition Height =Auto/>
< RowDefinition />
< /Grid.RowDefinitions>

< ComboBox ItemsSource ={Binding Path = Fonts}
SelectedItem ={Binding Path = SelectedFont,Mode = TwoWay}/>
< local:RichTextBoxCustom Grid.Row =1
CurrentFontFamily ={Binding Path = SelectedFont,Mode = TwoWay}
FontSize =30/>
< / Grid>
< / DockPanel>
< / Window>

以下是ViewModel:
如果您不使用视图模型,请告诉我我也将添加基类代码;否则,google / stackoverflow也可以帮助你。

  using System.Collections.ObjectModel; 
使用System.Windows.Media;

namespace RichTextboxFont.ViewModels
{
public class MainViewModel:ViewModelBase
{
#region构造函数

public MainViewModel()
{
FontFamily f1 = new FontFamily(Georgia);
_fonts.Add(f1);
FontFamily f2 = new FontFamily(Tahoma);
_fonts.Add(f2);
}

Private ObservableCollection< FontFamily> _fonts = new ObservableCollection< FontFamily>();
public ObservableCollection< FontFamily>字体
{
获得
{
return _fonts;
}
set
{
_fonts = value;
OnPropertyChanged(Fonts);


$ b $ private FontFamily _selectedFont = new FontFamily(Tahoma);
public FontFamily SelectedFont
{
get
{
return _selectedFont;
}
set
{
_selectedFont = value;
FontChanged = true;
OnPropertyChanged(SelectedFont);
}
}

private bool _fontChanged = false;
public bool FontChanged
{
get
{
return _fontChanged;
}
set
{
_fontChanged = value;
OnPropertyChanged(FontChanged);


$ b #endregion


code $ pre

这里是Window代码隐藏的地方,我使用System.Windows来初始化ViewModel:

  ; 

命名空间RichTextboxFont.Views
{
public partial class MainView:Window
{
public MainView()
{
InitializeComponent ();
this.DataContext = new ViewModels.MainViewModel();
}
}
}


I need to set the font family for the next text to be written in a RichTextBox. I tried setting that with...

<RichTextBox x:Name="RichTextEditor" MaxWidth="1000" SpellCheck.IsEnabled="True"
             FontFamily="{Binding ElementName=TextFontComboBox, Path=SelectedItem}"
             FontSize="{Binding ElementName=TextSizeComboBox, Path=SelectedValue}"
             Width="Auto" Height="Auto" HorizontalScrollBarVisibility="Auto" 
VerticalScrollBarVisibility="Auto" />

...but it changed the whole text. I suppose that with the Selection property I can restrict the change to be applied just to the selected area. But how for the next -not yet typed- text?

解决方案

In order to set the FontFamily based on the cursor position you need to define a custom control with a dependency property that helps insert a new Run section by overriding the OnTextInput method.

I included most of the code, you'll need to modify the namespaces to fit your development environment.

The code uses a ViewModel to manage the available fonts and manage if the font changed. This code is only a prototype and does not deal with focusing issues between the two controls.

To use this code:
1- Type some text in the RichTectBox.
2- Change the font in the ComboBox.
3- Tab back to the RichTextBox.
4- Type some more text.

Here is the custom RichTextBox control:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace RichTextboxFont.Views
{
  public class RichTextBoxCustom : RichTextBox
  {
    public static readonly DependencyProperty CurrentFontFamilyProperty =
            DependencyProperty.Register("CurrentFontFamily", 
            typeof(FontFamily), typeof  
            (RichTextBoxCustom), 
            new FrameworkPropertyMetadata(new FontFamily("Tahoma"), 
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
            new PropertyChangedCallback(OnCurrentFontChanged)));

    public FontFamily CurrentFontFamily
    {
       get
       {
         return (FontFamily)GetValue(CurrentFontFamilyProperty);
       }
       set
       {
         SetValue(CurrentFontFamilyProperty, value);
       }
    }

    private static void OnCurrentFontChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {}

    protected override void OnTextInput(TextCompositionEventArgs e)
    {
      ViewModels.MainViewModel mwvm = this.DataContext as ViewModels.MainViewModel;
      if ((mwvm != null) && (mwvm.FontChanged))
      {
        TextPointer textPointer = this.CaretPosition.GetInsertionPosition(LogicalDirection.Forward);
        Run run = new Run(e.Text, textPointer);
        run.FontFamily = this.CurrentFontFamily;
        this.CaretPosition = run.ElementEnd;
        mwvm.FontChanged = false;
      }
      else
      {
         base.OnTextInput(e);
      }
    }
  } 
}

Here is the XAML:

<Window x:Class="RichTextboxFont.Views.MainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:RichTextboxFont.Views" 
  xmlns:ViewModels="clr-namespace:RichTextboxFont.ViewModels" 
  Title="Main Window" 
  Height="400" Width="800">
  <DockPanel>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <ComboBox ItemsSource="{Binding Path=Fonts}" 
                  SelectedItem="{Binding Path=SelectedFont, Mode=TwoWay}"/>
        <local:RichTextBoxCustom Grid.Row="1" 
                                 CurrentFontFamily="{Binding Path=SelectedFont, Mode=TwoWay}" 
                                 FontSize="30"/>
    </Grid>
  </DockPanel>
</Window>

Here is the ViewModel: If you do not use view models, let me know and I'll add the base class code too; otherwise, google/stackoverflow can help you too.

using System.Collections.ObjectModel;
using System.Windows.Media;

namespace RichTextboxFont.ViewModels
{
  public class MainViewModel : ViewModelBase
  {
    #region Constructor

    public MainViewModel()
    {
       FontFamily f1 = new FontFamily("Georgia");
       _fonts.Add(f1);
       FontFamily f2 = new FontFamily("Tahoma");
       _fonts.Add(f2);
    }

    private ObservableCollection<FontFamily> _fonts = new ObservableCollection<FontFamily>();
    public ObservableCollection<FontFamily> Fonts
    {
      get
      {
         return _fonts;
      }
      set
      {
        _fonts = value;
        OnPropertyChanged("Fonts");
      }
    }

    private FontFamily _selectedFont = new FontFamily("Tahoma");
    public FontFamily SelectedFont
    {
      get
      {
        return _selectedFont;
      }
      set
      {
        _selectedFont = value;
        FontChanged = true;
        OnPropertyChanged("SelectedFont");
      }
    }

    private bool _fontChanged = false;
    public bool FontChanged
    {
      get
      {
         return _fontChanged;
      }
      set
      {
        _fontChanged = value;
        OnPropertyChanged("FontChanged");
      }
    }

  #endregion
  }
}

Here is the Window code-behind where I initialise the ViewModel:

using System.Windows;

namespace RichTextboxFont.Views
{
  public partial class MainView : Window
  {
    public MainView()
    {
      InitializeComponent();
      this.DataContext = new ViewModels.MainViewModel();
    }
  }
}

这篇关于如何设置RichTextBox字体的下一个文字被写入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆