嗨我长时间使用richtextbox.selection.applypropertyvalue()
函数时遇到错误,我第一次将它(单击按钮)应用到它不会这样做的richtextbox时它不会工作(我附上了一个图像.gif下面显示了更深入的问题)
以下是单击按钮时的代码,对于标签栏上的每个按钮/组合框都是相同的
CODE:
private void Button_Click(object sender, RoutedEventArgs e)
{
System.Windows.MessageBox.Show(textselectrangea.Text.Length.ToString());
if (textselectrangea.Text.Length != 0)
{
if (textselectrangea.GetPropertyValue(TextElement.FontWeightProperty).ToString() == "Normal" || textselectrangea.GetPropertyValue(TextElement.FontStyleProperty).ToString() == "{DependencyProperty.UnsetValue}")
{
boldbutton.FontWeight = FontWeights.Bold;
textselectrangea.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
}
else if (textselectrangea.GetPropertyValue(TextElement.FontWeightProperty).ToString() == "Bold")
{
boldbutton.FontWeight = FontWeights.Normal;
textselectrangea.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
}
}
//I think error occurs below here
else if (textselectrangea.Text.Length == 0)
{
if (richtextboxfile.Selection.GetPropertyValue(TextElement.FontWeightProperty).Equals(FontWeights.Normal))
{
boldbutton.FontWeight = FontWeights.Bold;
richtextboxfile.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
}
else if (richtextboxfile.Selection.GetPropertyValue(TextElement.FontWeightProperty).Equals(FontWeights.Bold))
{
boldbutton.FontWeight = FontWeights.Normal;
richtextboxfile.Selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
}
}
}
图片:
图片说明:此图片显示我必须先输入文字,然后才能在文字末尾设置文字属性。但是,如果我尝试通过单击按钮我再次输入文本然后再按下按钮(在照片中描述)
只是注意MessageBox只是一个检查选择长度的测试(不是错误)
答案 0 :(得分:1)
if (richtextboxfile.SelectedText.Length > 0) // If: there's any text selected toggle bold on that text
{
// Toggle bold on/off for selected text
if (richtextboxfile.SelectionFont.Style != FontStyle.Bold) // If: Current selected text is not bold, then bold it
{
richtextboxfile.SelectionFont = new Font(richtextboxfile.Font, FontStyle.Bold);
}
else // Else: selected text is bold, set it to regular text (non-bold)
{
richtextboxfile.SelectionFont = new Font(richtextboxfile.Font, FontStyle.Regular);
}
}
else // Else: No text is selected, make the font bold from here on out regardless of position
{
// if bold is not already enabled
if (richtextboxfile.SelectionFont.Style == FontStyle.Regular) // Toggle bold ON at current position
{
richtextboxfile.SelectionFont = new Font(richtextboxfile.Font, FontStyle.Bold);
}
else if (richtextboxfile.SelectionFont.Style == FontStyle.Bold) // Toggle bold OFF at current position
{
richtextboxfile.SelectionFont = new Font(richtextboxfile.Font, FontStyle.Regular);
}
}
答案 1 :(得分:1)
在按钮点击事件中使用Focus()方法。
if (!yourRichTextBox.IsFocused)
yourRichTextBox.Focus();
请找到附件。