2015-03-02 57 views
1

所有我見過的設置下劃線,上劃線或刪除線的例子都這個樣子:如何應用多個TextDecorations?

// setting underline 
textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, 
          TextDecorations.Underline); 

// clearing underline 
textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, null); 

這似乎過於簡單化了我;在TextDecorationsProperty返回裝飾的集合 - 你可以有OverlineUnderlineStrikethrough都在同一時間應用;像這樣設置它們會抹去整個系列。

這就是我對切換他們通過TextDecorationLocation

var textRange = new TextRange(tb.Selection.Start, tb.Selection.End); 
var tdp = textRange.GetPropertyValue(Inline.TextDecorationsProperty); 
var textDecorations = tdp.Equals(DependencyProperty.UnsetValue) 
         ? new TextDecorationCollection() 
         : tdp as TextDecorationCollection 
         ?? new TextDecorationCollection(); 

var strikethroughs = textDecorations.Where(d => d.Location == TextDecorationLocation.Strikethrough) 
            .ToList(); 
if (strikethroughs.Any()) 
{ 
    foreach (var strike in strikethroughs) 
    { 
     textDecorations.Remove(strike); 
    } 
} 
else 
{ 
    textDecorations.Add(TextDecorations.Strikethrough); 
} 

textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, textDecorations); 

這是一個很好的方式去了解這一點,還是我使它過於複雜?

+0

你願意純粹XAML解決方案? – XAMlMAX 2015-03-03 09:45:51

+0

道歉我誤解後,將XAML解決方案,我有隻允許一次在一個文本修飾。 – XAMlMAX 2015-03-03 10:16:31

回答

3

如果你試圖打開和關閉的裝飾品,同時允許它們被組合,你可以做到以下幾點:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <StackPanel Grid.Row="0" Orientation="Horizontal"> 
     <Button Content="Underline" Click="Underline" /> 
     <Button Content="Strikethrough" Click="Strikethrough" /> 
     <Button Content="Baseline" Click="Baseline" /> 
     <Button Content="Overline" Click="Overline" /> 
    </StackPanel> 
    <RichTextBox x:Name="tb" Grid.Row="1" /> 
</Grid> 

,然後在代碼

 private void Underline(object sender, RoutedEventArgs e) 
    { 
     this.SetDecorations(new TextRange(this.tb.Selection.Start, this.tb.Selection.End), TextDecorations.Underline); 
    } 

    private void Strikethrough(object sender, RoutedEventArgs e) 
    { 
     this.SetDecorations(new TextRange(this.tb.Selection.Start, this.tb.Selection.End), TextDecorations.Strikethrough); 
    } 

    private void Baseline(object sender, RoutedEventArgs e) 
    { 
     this.SetDecorations(new TextRange(this.tb.Selection.Start, this.tb.Selection.End), TextDecorations.Baseline); 
    } 

    private void Overline(object sender, RoutedEventArgs e) 
    { 
     this.SetDecorations(new TextRange(this.tb.Selection.Start, this.tb.Selection.End), TextDecorations.OverLine); 
    } 

    private void SetDecorations(TextRange textRange, TextDecorationCollection decoration) 
    { 
     var decorations = textRange.GetPropertyValue(Inline.TextDecorationsProperty) as TextDecorationCollection 
          ?? new TextDecorationCollection(); 

     decorations = decorations.Contains(decoration.First()) 
          ? new TextDecorationCollection(decorations.Except(decoration)) 
          : new TextDecorationCollection(decorations.Union(decoration)); 

     textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, decorations); 
    } 

該代碼使用現有裝飾集,然後或者工會或設置節選基於是否它已附加電流裝飾 - 允許所述裝飾被開/關切換。

+0

我喜歡使用Except和Union。非常清晰和簡潔。 – 2015-03-03 23:39:24

相關問題