当前位置:网站首页 / WPF / 正文

写给自己的WPF4.5 27[文档 1/2]

时间:2015年07月22日 | 作者 : aaronyang | 分类 : WPF | 浏览: 2602次 | 评论 0

======================================== AYUI       www.ayjs.net      AY         杨洋原创编写,请不要转载谢谢===============

文档: 固定文档(已经排好版、准备打印的文档,例如pdf,xps)和流文档(在计算机查看而设计的文档)

DocumentViewer容器允许在WPF窗口显示固定文档。FlowDocumentReader、FlowDocumentPageViewer以及 FlowDocumentScrollViewer容器提供了查看流文档的不同方式。容器都是只读。WPF允许编程方式创建固定文档,也允许使用RichTextBox控件编辑流内容。

展示

默认填充屏幕,类似wrappanel换行。  或者你可以使用Grid,3列布局,中间放内容,类似网页开发960px,WPF的流文档内容支持分页、多列显示、高级连接字符和文档流算法,以及用户能够调整查看选择。

流文档中的元素,你可以理解流文档相当于浏览器,元素相当于html标签。

流内容元素继承ContentElement和 FrameworkContentElement类,不是UIElement和FrameworkElement。默认流元素的focusable是false,不能接受焦点,你可以更改让它支持焦点。Hyberlink是一个Focuable=true的例子。

Image 3.png

两个重要的分支:

Block元素:用于分组其他内容元素。例如Paragraph元素是块级别元素,能够包含以各种不同的方式格式化的文本。在段落中被格式化的每部分文本都是不同的元素。

Inline元素:这些元素是被嵌入到块级别元素(或另一个内联级别元素)例如,Run元素封装了一些文本,之后Run又被嵌入到Paragraph元素中。

就类似网页的 Display属性        inline还是block

元素就是一层一层嵌套显示的,所有这些元素都在System.Windows.Documents名称定义

内容元素的常用属性:Foreground,Background,FontFamliy、FontSize、FontStretch,FontStyle、FontWeight、ToolTip、Style

块状:BorderBrush,BorderThickness、Margin,Padding、TextAlignment、LineHeight、LineStackingStrategy(默认:MaxHeight,还有BlockLineHeight)

TextDecorations属性:Paragraph和所有继承Inline的元素都有这个属性。Typography属性,顶级的FlowDocument提供了该属性,TextBlock元素以及所有继承自TextElement类型的元素也具有该属性。该属性可修改各种有关文本呈现方式的细节(大多数只应用OpenType字体)。

Demo1    基本

<Window x:Class="Ay_RichText.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Ay_RichText"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Height="40"></StackPanel>
        <FlowDocumentScrollViewer Name="docViewer" IsToolBarVisible="True" >
            <FlowDocument>
                <Paragraph>Hello Ay Document</Paragraph>
            </FlowDocument>
        </FlowDocumentScrollViewer>
    </DockPanel>
</Window>

其中IsToolBarVisible是控制下方的栏目的


当然你可以写多个,就像html一样

 <FlowDocument>
                <Paragraph>Hello Ay Document</Paragraph>
                <Paragraph>第一个wpf流文档</Paragraph>
            </FlowDocument>
  <FlowDocumentScrollViewer Name="docViewer" IsToolBarVisible="True" IsSelectionEnabled="False">
            <FlowDocument>
                <Paragraph>Hello Ay Document</Paragraph>
                <Paragraph>第一个wpf流文档</Paragraph>
            </FlowDocument>
        </FlowDocumentScrollViewer>

默认FlowDocumentScrollViewer中的文本可以选择,方便复制粘贴,可以通过IsSelectionEnabled=false禁止

Paragraph没有Text属性,如果想获得值,里面的文本可以使用Run嵌套文本

文档外添加个按钮,单击事件代码如下

Image 10.png

此时的前台代码如下:给paragraph取了个名字,Paragraph的内容都存储在Inlines集合中。

<Window x:Class="Ay_RichText.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Ay_RichText" WindowStartupLocation="CenterScreen"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Height="40">
            <Button Content="获得paragraph的文本" Width="130" Height="40" HorizontalAlignment="Left" Click="Button_Click"/>
        </StackPanel>
        <FlowDocumentScrollViewer Name="docViewer" IsToolBarVisible="True" IsSelectionEnabled="False">
            <FlowDocument>
                <Paragraph x:Name="fdTitle">
                    <Run>标题:Ay wpf 文档学习</Run>
                </Paragraph>
                <Paragraph>Hello Ay Document</Paragraph>
                <Paragraph>第一个wpf流文档</Paragraph>

            </FlowDocument>
        </FlowDocumentScrollViewer>
    </DockPanel>
</Window>

后台改变值

   private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ((Run)fdTitle.Inlines.FirstInline).Text = "哈哈,ay的新标题,快看!";
        }

1.gif

其他基础

斜体,加粗,可以互相嵌套

Image 16.png

WPF的没有html中的h1,h2...h6的标题,wpf中你可以设置字体大小来实现。

List元素

Image 17.png

  <Paragraph>AYUI 3.3 购买说明</Paragraph>
                <List MarkerStyle="Decimal">
                    <ListItem>
                        <Paragraph>
                            学习版A 700(商业不授权,不免费更新,无企业部分,无海康威视,好评返100,无ArtChart控件)
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            学习版A+ 1100(商业不授权,免费更新,无企业部分,无海康威视,不免费更新,好评返100,有ArtChart控件)
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            学习版 A1 1400 (商业不授权,免费更新,有海康监控wpf版本,拥有企业开发附加包,一次购买免费更新,提供框架帮助咨询,好评返100)
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            企业版A  2000(商业授权,无海康监控,拥有企业开发附加包,一次购买免费更新,提供框架帮助咨询,好评返100)
                        </Paragraph>
                    </ListItem>
                    <ListItem>
                        <Paragraph>
                            企业版A+2200 (商业授权,有海康监控wpf版本,拥有企业开发附加包,一次购买免费更新,提供框架帮助咨询,好评返100)
                        </Paragraph>
                    </ListItem>
                </List>

效果图:

Image 19.png

table元素,表格布局内容

  <Paragraph>Table方式AYUI 3.3 购买说明</Paragraph>
                <Table>
                    <TableRowGroup Paragraph.TextAlignment="Center">
                        <TableRow FontWeight="Bold">
                            <TableCell>
                                <Paragraph>版本</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>套餐</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>价格</Paragraph>
                            </TableCell>
                        </TableRow>
                    
                        <TableRow>
                            <TableCell>
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>学习版A</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>700</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell>
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>学习版A+</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>1100</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell>
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>学习版A1</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>1400</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell>
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>学习版A+</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>1100</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell>
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>学习版A1</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>1400</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell>
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>企业版A</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>2000</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell>
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>企业版A+</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>2200</Paragraph>
                            </TableCell>
                        </TableRow>

                    </TableRowGroup>
                </Table>

效果:

Image 23.png

表格列宽,要不就是*号,比例方式布局,要不就是具体值,不可以两个都结合使用,不像Grid那种

Image 24.png

表格增加 边框和颜色

                <Table BorderBrush="Red" BorderThickness="1">

如果我想有边框怎么办,我的解决办法就是,外边框,左边和上面有,然后cell是右边和下边。

    <Table BorderBrush="Red" BorderThickness="1,1,0,0" CellSpacing="0" >
                    <Table.Columns>
                        <TableColumn Width="3*"/>
                        <TableColumn Width="2*"/>
                        <TableColumn Width="*"/>
                    </Table.Columns>
                    <TableRowGroup Paragraph.TextAlignment="Center">
                        <TableRow FontWeight="Bold" >
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>版本</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>套餐</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>价格</Paragraph>
                            </TableCell>
                        </TableRow>
                    
                        <TableRow>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>学习版A</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>700</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>学习版A+</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>1100</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>学习版A1</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>1400</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>学习版A+</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>1100</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>学习版A1</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>1400</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>企业版A</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>2000</Paragraph>
                            </TableCell>
                        </TableRow>
                        <TableRow>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>3.3</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>企业版A+</Paragraph>
                            </TableCell>
                            <TableCell BorderBrush="Red" BorderThickness="0,0,1,1">
                                <Paragraph>2200</Paragraph>
                            </TableCell>
                        </TableRow>

                    </TableRowGroup>
                </Table>

效果图:

Image 25.png

Section元素:用于把其他元素组合到一起,比如把多个paragraph组合一起,然后Section.Background,相当于描底色

Image 26.png

     <Section Background="Yellow" FontSize="16" FontWeight="Light" Foreground="Green"
                         FontFamily="隶书,微软雅黑" TextAlignment="Right">
                    <Paragraph>
                        <Run>我很聪明</Run>
                    </Paragraph>
                    <Paragraph>
                        <Run>我也很聪明</Run>
                    </Paragraph>
                    <Paragraph>
                        <Run>你们都没我聪明</Run>
                    </Paragraph>
                </Section>

效果图:因为每个paragraph的背景默认都是透明的,所以看到了黄色。当然Section也有Style,你可以设置一个style资源

Image 27.png

BlockUIContainer: 可以放置块级别元素,放置非内容元素(继承自UIElement的类),这样就可以放 Button,复选框等

          <BlockUIContainer>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox Height="30" VerticalAlignment="Center">ay的BlockUIContainer</CheckBox>
                        <Button Content="www.ayjs.net" Width="120"  VerticalAlignment="Center" Height="30" Background="GreenYellow"></Button>
                    </StackPanel>
                </BlockUIContainer>

效果图:

Image 28.png

内联元素,Run,Bold,Italic,Underline就不说了,说说其他的

Span:包含任意数量的其他内联元素,通常格式化一部分文本,跟Section类似,跟Html中的span类似

Hyberlink:超链接

LineBreak:在块级别元素中添加换行符。可优先考虑使用margin或者padding,再决定

InlineUIContainer: 内联方式的  放UI元素

Floater和Figure,通过这两个元素可以嵌入一块用于强调重要信息的浮动内容,显示一副图片或者显示关联的内容(如广告、链接或者代码列表)

保留空白符号:如果连续多个空白符号,或者tab键,会变成单个空白,可以使用preserve

 <Section Background="Yellow" FontSize="16" FontWeight="Light" Foreground="Green"
                         FontFamily="隶书,微软雅黑" TextAlignment="Right">
                    <Paragraph>
                        <Run>我很聪明</Run>
                    </Paragraph>
                    <Paragraph>
                        <Run>我也很聪明</Run>
                    </Paragraph>
                    <Paragraph>
                        <Run>你们都没我聪明</Run>
                    </Paragraph>
                    <Paragraph xml:space="preserve">
                        <Run>你们都没我聪明    我        真的    也很聪明    </Run>
                    </Paragraph>
                </Section>

最后几个空白符号也会保留

Image 29.png

Floater的使用

新建个简单样式,作用于Paragraph

    <Window.Resources>
        <Style x:Key="AyParagraphDemoStyle">
            <Setter Property="Paragraph.FontSize" Value="20"></Setter>
            <Setter Property="Paragraph.Padding" Value="6"></Setter>
            <Setter Property="Paragraph.Margin" Value="4,10"></Setter>
        </Style>
    </Window.Resources>

然后使用Floater,让块中的元素浮动

  <Paragraph>
                    <Run>在IT界,有一个快乐的程序员,叫</Run>
                    <LineBreak></LineBreak>
                    <Floater Style="{StaticResource AyParagraphDemoStyle}" Width="205" HorizontalAlignment="Left">
                        <Paragraph>AY 杨洋 安徽 合肥</Paragraph></Floater>
                    ,他每天晚上都学习,学到很晚,希望每天能有成长,大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习
                </Paragraph>

效果图:

Image 30.png

Floater默认是占一行的,你可以设置宽度,等其他信息。这里我是用了LineBreak换行了。相当于网页的Br

显示图片

还可以使用Floater嵌套个图片显示,但是没有流元素支持,相反,使用BlockUIContainer或者InlineUIContainer然后包装Image控件显示。有些拉伸,占行,稍微注意下就能解决。

找一张图片过来,例如我的叫ay.png

         <Paragraph>
                    <Run>在IT界,有一个快乐的程序员,叫AY(杨洋 安徽 合肥)</Run>
                    <Floater Width="100" Padding="5,0,5,0" HorizontalAlignment="Left">
                        <BlockUIContainer>
                            <Image Source="ay.png"/>
                        </BlockUIContainer>
                    </Floater>
                    ,他每天晚上都学习,学到很晚,希望每天能有成长,大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习
                </Paragraph>

运行项目效果如下

Image 34.png

文字周围会自动换行。注意,这里的图片会随着文档的 缩放特性,然后结合自己的Image的Stretch属性,调整自己。

Figure:和Floater类似,但是能够更好的控制位置

Figure提供的属性:Width,Height,HorizontalAnchor,VerticalAnchor,HorizontalOffset,VerticalOffset、WrapDirection(确定是否允许在图形的一侧或者两侧换行文本-允许空格)

参考代码:

  <Paragraph>
                    <Run>在IT界,有一个快乐的程序员,叫AY(杨洋 安徽 合肥)</Run>
                    <Floater Width="100" Padding="5,0,5,0" HorizontalAlignment="Left">
                        <BlockUIContainer>
                            <Image Source="ay.png"/>
                        </BlockUIContainer>
                    </Floater>
                    ,他每天晚上都学习,学到很晚,希望每天能有成长,大家一起来学习。。。
                    <Figure Width="100" Padding="5,0,5,0" HorizontalAnchor="PageRight">
                        <BlockUIContainer>
                            <Image Source="ay.png" ></Image>
                        </BlockUIContainer>
                    </Figure> 大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习。。。大家一起来学习
                </Paragraph>

Image 35.png

效果感觉Figure更好控制。对其方式也舒服点。

后台创建简单文档代码

上方添加一个按钮,然后添加事件

代码如下:

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Run run1 = new Run();
            run1.Text = "Ay目前在研究";
            Run run2 = new Run();
            run2.Text = "WPF";
            Bold b = new Bold();
            b.Inlines.Add(run2);
            Run run3 = new Run();
            run3.Text = ",已经有3年了";
            Paragraph p = new Paragraph();
            p.Inlines.Add(run1);
            p.Inlines.Add(b);
            p.Inlines.Add(run3);
            FlowDocument document = new FlowDocument();
            document.Blocks.Add(p);
            docViewer.Document = document;
        }

效果图:

4.gif

Block的快速方法

比如FlowDocument.Blocks集合,使用block.FirstBlock第一个,LastBlock最后一个,使用NextBlock,从一个块级别元素移动下一个或者上一个元素Block.Previous属性,使用Block.SiblingBlocks集合浏览同级    所有块级别元素。包含其他的元素的,比如List有ListItem,Table的TableRowGroup,Paragraph的Inlines集合。

下面有个复杂的创建一段,用户的 自己填写的 段落,参考下吧,他在动态改变的值的地方加上了Tag

前台定义:

<FlowDocumentScrollViewer Grid.Row="2" Name="docViewer" Visibility="Hidden">
    <FlowDocument Name="document">
      <Paragraph FontSize="20" FontWeight="Bold">
        Release Notes
      </Paragraph>
      <Paragraph>
        These are the release <Span Tag="Plural Noun">notes</Span> for
        <Span Tag="Proper Noun">Linux</Span> version 1.2.13.
      </Paragraph>
      <Paragraph>
        Read them <Span Tag="Adverb">carefully</Span>, as they
        tell you what this is all about, how to
        <Span Tag="Verb">boot</Span> the
        <Span Tag="Noun">kernel</Span>,
        and what to do if something goes wrong.
      </Paragraph>
    </FlowDocument>
  </FlowDocumentScrollViewer>

后台替换的事件

  private void WindowLoaded(Object sender, RoutedEventArgs e)
        {
            // Clear grid of text entry controls.
            gridWords.Children.Clear();

            // Look at paragraphs.
            foreach (Block block in document.Blocks)
            {
                Paragraph paragraph = block as Paragraph;

                // Look for spans.
                foreach (Inline inline in paragraph.Inlines)
                {
                    Span span = inline as Span;
                    if (span != null)
                    {
                        RowDefinition row = new RowDefinition();
                        gridWords.RowDefinitions.Add(row);

                        Label lbl = new Label();
                        lbl.Content = inline.Tag.ToString() + ":";
                        Grid.SetColumn(lbl, 0);
                        Grid.SetRow(lbl, gridWords.RowDefinitions.Count - 1);
                        gridWords.Children.Add(lbl);

                        TextBox txt = new TextBox();
                        Grid.SetColumn(txt, 1);
                        Grid.SetRow(txt, gridWords.RowDefinitions.Count - 1);
                        gridWords.Children.Add(txt); 
                                               
                        txt.Tag = span.Inlines.FirstInline;
                    }
                }
            }
        }

        private void cmdGenerate_Click(Object sender, RoutedEventArgs e)
        {
            foreach (UIElement child in gridWords.Children)
            {
                if (Grid.GetColumn(child) == 1)
                {
                    TextBox txt = (TextBox)child;

                    if (txt.Text != "") ((Run)txt.Tag).Text = txt.Text;
                }
            }
            docViewer.Visibility = Visibility.Visible;
        }

文本对齐

通常禁用WPF的最佳段落布局特性。也许,使用布局算法会有额外的开销。大多数情况下都不会有影响(窗体改变大小,重新布局等)。

启用最佳布局,只要设置FlowDocument的IsOptimalParagraphEnabled="True"即可,效果对比,下面使用了对齐算法

Image 36.png

为了进一步改善文本对齐,特别比较窄的窗口中,可以设置IsHyphenationEnabled="True" 。设置后WPF会在需要的地方分隔较长的单词,从而在单词之间保留较小的空间。当使用多列布局的时候,这个就很重要了。WPF使用断字字典确保连字符位于合适的位置(在两个音节之间)

www.ayjs.net独家拥有,未经许可,不许转载,违者追究法律责任


推荐您阅读更多有关于“WPF4.5,”的文章

猜你喜欢

额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

  查看权限

抖音:wpfui 工作wpf,目前主maui

招聘合肥一枚WPF工程师,跟我一个开发组,10-15K,欢迎打扰

目前在合肥市企迈科技就职

AYUI8全源码 Github地址:前往获取

杨洋(AaronYang简称AY,安徽六安人)AY唯一QQ:875556003和AY交流

高中学历,2010年开始web开发,2015年1月17日开始学习WPF

声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费

不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo

查看捐赠

AYUI7.X MVC教程 更新如下:

第一课 第二课 程序加密教程

标签列表