专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Silverlight

怎么用代码设置RichTextBox的下划线

发布时间:2011-06-27 19:29:03 文章来源:www.iduyao.cn 采编人员:星星草
如何用代码设置RichTextBox的下划线?
wpf的RichTextBox中没有textdecorations这一属性,如何设置?
------解决方案--------------------
<Window x:Class="MyTestWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyTestWpf"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <RichTextBox x:Name="richTextBox1" HorizontalAlignment="Left" Height="206" Margin="37,32,0,0" VerticalAlignment="Top" Width="195">
            <FlowDocument>
                <Paragraph>
                    <Run Text="RichTextBox"/>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
        <Button Content="Button" HorizontalAlignment="Left" Margin="281,61,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>

using System;
using System.Linq;
using System.Windows;
using System.Windows.Documents;

namespace MyTestWpf
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var selection = this.richTextBox1.Selection;
            if (!selection.IsEmpty) {
                var tdc = (TextDecorationCollection)selection.GetPropertyValue(Inline.TextDecorationsProperty);
                if (tdc == null 
------解决方案--------------------
 !tdc.SequenceEqual(TextDecorations.Underline)) {
                    selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
                } else {
                    selection.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
                }
            } else {
                var textRange = new TextRange(this.richTextBox1.Document.ContentStart, this.richTextBox1.Document.ContentEnd);
                if (!textRange.IsEmpty) {
                    textRange.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
                }
            }
        }
    }

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: