我按照本教程创建了一个富文本编辑器:How-to: Creating a Rich Text Editor
粗体和斜体按预期工作,但下划线会引发错误。我不明白为什么并希望有人对此有所了解!
工具栏和RichTextBox中的Togglebuttons:
<ToolBar>
<ToggleButton Command="EditingCommands.ToggleBold" Name="btnBold" Height="25" Width="20" HorizontalAlignment="Left" Content="F" FontWeight="ExtraBold"/>
<ToggleButton Command="EditingCommands.ToggleItalic" Name="btnItalic" Height="25" Width="20" HorizontalAlignment="Left" Content="K" FontStyle="Oblique"/>
<ToggleButton Visibility="Hidden" Command="EditingCommands.ToggleUnderline" Name="btnUnderline" Height="25" Width="20" HorizontalAlignment="Left">
<ToggleButton.Content>
<TextBlock TextDecorations="Underline" Text="U"/>
</ToggleButton.Content>
</ToggleButton>
<ComboBox Name="cmbFontSize" Height="20" Width="50" IsEditable="True" TextBoxBase.TextChanged="cmbFontSize_TextChanged" />
</ToolBar>
<RichTextBox Name="rtbEditor" SelectionChanged="rtbEditor_SelectionChanged"/>
Sub使用按钮:
Public Sub rtbEditor_SelectionChanged(sender As Object, e As RoutedEventArgs)
Dim temp As Object = rtbEditor.Selection.GetPropertyValue(Inline.FontWeightProperty)
btnBold.IsChecked = (temp <> Nothing) AndAlso (temp.Equals(FontWeights.Bold))
temp = rtbEditor.Selection.GetPropertyValue(Inline.FontStyleProperty)
btnItalic.IsChecked = (temp <> Nothing) AndAlso (temp.Equals(FontStyles.Italic))
temp = rtbEditor.Selection.GetPropertyValue(Inline.TextDecorationsProperty)
btnUnderline.IsChecked = (temp <> DependencyProperty.UnsetValue) AndAlso (temp.Equals(TextDecorations.Underline))
temp = rtbEditor.Selection.GetPropertyValue(Inline.FontSizeProperty)
cmbFontSize.Text = temp.ToString()
End Sub
这会引发错误:
btnUnderline.IsChecked = (temp <> DependencyProperty.UnsetValue) AndAlso (temp.Equals(TextDecorations.Underline))
错误讯息(翻译自瑞典语):
运营商&lt;&gt;没有为TextDecorationCollection和NamedObject
类型定义
为什么?
答案 0 :(得分:0)
使用:
if blubb isNot nothing
End if
而不是:
if blubb <> nothing
End if
您的代码:
Public Sub rtbEditor_SelectionChanged(sender As Object, e As RoutedEventArgs)
Dim temp As Object = rtbEditor.Selection.GetPropertyValue(Inline.FontWeightProperty)
btnBold.IsChecked = (temp isNot Nothing) AndAlso (temp.Equals(FontWeights.Bold))
temp = rtbEditor.Selection.GetPropertyValue(Inline.FontStyleProperty)
btnItalic.IsChecked = (temp isNot Nothing) AndAlso (temp.Equals(FontStyles.Italic))
temp = rtbEditor.Selection.GetPropertyValue(Inline.TextDecorationsProperty)
btnUnderline.IsChecked = (temp isNot DependencyProperty.UnsetValue) AndAlso (temp.Equals(TextDecorations.Underline))
temp = rtbEditor.Selection.GetPropertyValue(Inline.FontSizeProperty)
cmbFontSize.Text = temp.ToString()
End Sub