Hallo zusammen
der Code zum ändern der Richtextbox funktioniert
ich brauch die ToggleButton etc. aber ohne ToolBar
sobald ich sie aus der Toolbar ziehe funktionieren die ToggleButton nicht mehr
ich denke mir das hängt mit dem 'Command' zusammen aber ich komm nicht drauf
besten Dank Alex
<Window x:Class="MainWin"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWin" Height="371" Width="595" Loaded="Window_Loaded">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Open" _
Executed="Open_Executed" />
<CommandBinding Command="ApplicationCommands.Save" _
Executed="Save_Executed" />
</Window.CommandBindings>
<Grid>
<ToolBar Margin="0,0,126,305">
<Button Command="ApplicationCommands.Open" Content="Open" />
<Button Command="ApplicationCommands.Save" Content="Save"/>
<Separator />
<ToggleButton Command="EditingCommands.ToggleBold" x:Name="btnBold" _
Content="Fett" />
<ToggleButton Command="EditingCommands.ToggleItalic" _
x:Name="btnItalic" Content="kursiv" />
<ToggleButton Command="EditingCommands.ToggleUnderline" _
x:Name="btnUnderline" Content="unterstrichen" FontFamily="Arial" />
<Separator />
<ComboBox x:Name="cmbFontFamily" Width="150" _
SelectionChanged="cmbFontFamily_SelectionChanged" />
<ComboBox x:Name="cmbFontSize" Width="50" IsEditable="True" _
TextBoxBase.TextChanged="cmbFontSize_TextChanged" />
</ToolBar>
<RichTextBox x:Name="rtbEditor" _
SelectionChanged="rtbEditor_SelectionChanged" Margin="45,117,240,71" />
</Grid>
</Window> Imports System.IO
Imports Microsoft.Win32
Public Class MainWin
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
cmbFontFamily.ItemsSource = Fonts.SystemFontFamilies.OrderBy(Function( _
f) f.Source)
cmbFontSize.ItemsSource = New List(Of Double)() From {8, 9, 10, 11, 12, _
14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72}
End Sub
Private Sub rtbEditor_SelectionChanged(sender As Object, e As _
RoutedEventArgs)
Dim temp As Object = rtbEditor.Selection.GetPropertyValue( _
Inline.FontWeightProperty)
btnBold.IsChecked = (temp IsNot DependencyProperty.UnsetValue) AndAlso ( _
temp.Equals(FontWeights.Bold))
temp = rtbEditor.Selection.GetPropertyValue(Inline.FontStyleProperty)
btnItalic.IsChecked = (temp IsNot DependencyProperty.UnsetValue) _
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.FontFamilyProperty)
cmbFontFamily.SelectedItem = temp
temp = rtbEditor.Selection.GetPropertyValue(Inline.FontSizeProperty)
cmbFontSize.Text = temp.ToString()
End Sub
Private Sub Open_Executed(sender As Object, e As ExecutedRoutedEventArgs)
Dim dlg As New OpenFileDialog()
dlg.Filter = "Rich Text Format (*.rtf)|*.rtf|All files (*.*)|*.*"
If dlg.ShowDialog() = True Then
Dim fileStream As New FileStream(dlg.FileName, FileMode.Open)
Dim range As New TextRange(rtbEditor.Document.ContentStart, _
rtbEditor.Document.ContentEnd)
range.Load(fileStream, DataFormats.Rtf)
End If
End Sub
Private Sub Save_Executed(sender As Object, e As ExecutedRoutedEventArgs)
Dim dlg As New SaveFileDialog()
dlg.Filter = "Rich Text Format (*.rtf)|*.rtf|All files (*.*)|*.*"
If dlg.ShowDialog() = True Then
Dim fileStream As New FileStream(dlg.FileName, FileMode.Create)
Dim range As New TextRange(rtbEditor.Document.ContentStart, _
rtbEditor.Document.ContentEnd)
range.Save(fileStream, DataFormats.Rtf)
End If
End Sub
Private Sub cmbFontFamily_SelectionChanged(sender As Object, e As _
SelectionChangedEventArgs)
If cmbFontFamily.SelectedItem IsNot Nothing Then
rtbEditor.Selection.ApplyPropertyValue(Inline.FontFamilyProperty, _
cmbFontFamily.SelectedItem)
End If
End Sub
Private Sub cmbFontSize_TextChanged(sender As Object, e As _
TextChangedEventArgs)
rtbEditor.Selection.ApplyPropertyValue(Inline.FontSizeProperty, _
cmbFontSize.Text)
End Sub
End Class |