文本下划线检测会导致错误 - 为什么?
Text underline detection causes an error - why?
我按照本教程创建了一个富文本编辑器:How-to: Creating a Rich Text Editor
粗体和斜体按预期工作,但下划线会引发错误。我不明白为什么,希望有人对此有所了解!
工具栏和 RichTextBox 中的切换按钮:
<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"/>
子使用按钮:
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))
错误消息(翻译自瑞典语):
operator <> not defined for type TextDecorationCollection and NamedObject
为什么??
使用:
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
我按照本教程创建了一个富文本编辑器:How-to: Creating a Rich Text Editor
粗体和斜体按预期工作,但下划线会引发错误。我不明白为什么,希望有人对此有所了解!
工具栏和 RichTextBox 中的切换按钮:
<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"/>
子使用按钮:
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))
错误消息(翻译自瑞典语):
operator <> not defined for type TextDecorationCollection and NamedObject
为什么??
使用:
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