Рендеринг FlowDocument не обновляется при изменении IsHyphenationEnabled - PullRequest
0 голосов
/ 01 октября 2019

My FlowDocument не обновляется, когда свойства IsHyphenationEnabled и IsOptimalParagraphEnabled изменены, как показано на снимке экрана и с кодом ниже:

enter image description here

<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 #:

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 я не вижу, чтобы свойства статически определялись при первом отображении документа. Я что-то пропустил?

1 Ответ

0 голосов
/ 01 октября 2019

Просто осознав, что мои свойства не связаны с использованием объектов Binding. Если кто-то совершит ту же ошибку новичка:

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"));

Может быть, это можно упростить / оптимизировать.

...