vs2010,WPF .NET 4.5。
我认为我有一个RichTextBox
。文本设置为Arial,大小为12:
<xctk:RichTextBox DataContext="{StaticResource EditorViewModel}" Grid.Row="1"
Height="296" HorizontalAlignment="Center" SpellCheck.IsEnabled="True" Margin="6,145,6,0"
Name="richTextBoxArticleBody" VerticalAlignment="Top" Width="962" Grid.RowSpan="2"
BorderBrush="Silver" BorderThickness="1" AcceptsTab="True" FontFamily="Arial" FontSize="12"
Text="{Binding PastedText, UpdateSourceTrigger=PropertyChanged}" />
我想将所有格式从粘贴中删除到我的RichTextBox
。我的视图中有一个粘贴按钮,该按钮绑定到FormatPastedText
命令:
private void FormatPastedTextCommandAction()
{
string paste = (string)Clipboard.GetData("Text");
Clipboard.SetText(paste);
PastedText += paste.ToString();
Clipboard.Clear();
}
这几乎可以正常工作,除了粘贴的文本不是以字体大小12显示,而是以大约15显示。键入的文本按照预期格式化为12。有没有更好的方法来设置粘贴字符串的字体大小?
由于
答案 0 :(得分:0)
试试这个,这可能会解决您的问题。这可能无法解决问题。但是代码中提到的代码有一些改进。
private void FormatPastedTextCommandAction()
{
string paste = Clipboard.GetText(); // casting is not required if this function is used
// Clipboard.SetText(paste); // This line is reduntant
PastedText += paste; // no need to call ToString()
// Clipboard.Clear(); // You should not clear the Clipboard, as the user may want to paste the data in some other window/application.
}
答案 1 :(得分:0)
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.V) && e.Control && !e.Alt && !e.Shift)
{
// remove text formatting in the text in clipboard
if (Clipboard.ContainsText(TextDataFormat.Html) || Clipboard.ContainsText(TextDataFormat.Rtf))
{
string plainText = Clipboard.GetText();
Clipboard.Clear();
Clipboard.SetText(plainText);
}
}
}