首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当IsHyphenationEnabled更改时,FlowDocument渲染未更新

当IsHyphenationEnabled更改时,FlowDocument渲染未更新
EN

Stack Overflow用户
提问于 2019-10-01 17:52:15
回答 1查看 185关注 0票数 0

当属性IsHyphenationEnabledIsOptimalParagraphEnabled在屏幕截图和以下代码中可见时,我的FlowDocument不会更新:

代码语言:javascript
复制
<Window x:Class="FlowDocumentDemo.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:FlowDocumentDemo"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="700" Width="300">

    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,0,0,20">
            <CheckBox Margin="8" Content="Hyphenation" IsChecked="{Binding Hyphenation}"/>
            <CheckBox Margin="8" Content="Optimal Paragraphs" IsChecked="{Binding OptimalParagraphs}"/>
        </StackPanel>
        <FlowDocumentReader Grid.Row="1" ViewingMode="Scroll" Document="{Binding Document}"/>
    </Grid>
</Window>

C#:

代码语言:javascript
复制
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

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

    class ViewModel : INotifyPropertyChanged {
        protected FlowDocument document;
        protected bool hyphenation = true;
        protected bool optimalParagraphs = true;

        public event PropertyChangedEventHandler PropertyChanged;

        public FlowDocument Document { get => document; set { document = value; RaisePropertyChanged (); } }
        public bool Hyphenation { get => hyphenation; set { hyphenation = value; RaisePropertyChanged (); } }
        public bool OptimalParagraphs { get => optimalParagraphs; set { optimalParagraphs = value; RaisePropertyChanged (); } }

        public ViewModel () {
            Document = new FlowDocument {
                ColumnWidth = Double.MaxValue,
                TextAlignment = TextAlignment.Justify,
                FontFamily = new FontFamily ("Arial"),
                IsHyphenationEnabled = Hyphenation,
                IsOptimalParagraphEnabled = OptimalParagraphs
            };

            Paragraph paragraph = new Paragraph ();
            paragraph.Inlines.Add (new Run {
                FontSize = 35,
                FontWeight = FontWeights.Bold,
                Text = "NASA announces funding for moon and Mars mission tech"
            });
            Document.Blocks.Add (paragraph);

            paragraph = new Paragraph ();
            paragraph.Inlines.Add (new Run {
                FontFamily = new FontFamily ("Arial"),
                Text = "NASA announced agreements worth a combined $43.2 million with 14 commercial partners " +
                       "Friday — including Blue Origin and SpaceX — to fund experiments in propellant and power " +
                       "generation, in-space refueling, efficient propulsion systems, and lunar rover technology."
            });
            Document.Blocks.Add (paragraph);
        }

        protected void RaisePropertyChanged ([CallerMemberName] string propertyName = null) {
            PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
        }
    }
}

MSDN中,我没有看到属性是在文档第一次显示时静态定义的。我错过了什么吗?

EN

回答 1

Stack Overflow用户

发布于 2019-10-01 18:22:49

我只是意识到我的属性不是使用绑定对象绑定的。以防有人犯了同样的初学者错误:

代码语言:javascript
复制
public ViewModel () {
        Document = new FlowDocument {
            ColumnWidth = Double.MaxValue,
            TextAlignment = TextAlignment.Justify,
            FontFamily = new FontFamily ("Arial"),
        };
        Document.SetBinding (FlowDocument.IsHyphenationEnabledProperty, new Binding ("Hyphenation"));
        Document.SetBinding (FlowDocument.IsOptimalParagraphEnabledProperty, new Binding ("OptimalParagraphs"));

也许这可以被简化/优化。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58182267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档