質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

1回答

2641閲覧

WPF でFlowDocument の表示で任意の位置で改ページさせたい

byori

総合スコア71

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

1グッド

0クリップ

投稿2021/02/04 07:20

編集2021/02/05 00:43

WPF でFlowDocumentを使ってデータを表示させようとしています。

表示は出来ているのですが、表示を任意のところで改ページさせたいと考えていますが、
どうやったら任意の個所でページを切り替えられますか?

C#

1<Window x:Class="ReportLite3.Views.PrevReportWindow" 2 : 3 4 <Grid> 5 <FlowDocumentReader Name="flowDoc" MinZoom="50" MaxZoom="300" Zoom="120" ZoomIncrement="5" /> 6 </Grid> 7</Window> 8 9ビハインド側 10 FlowDocument fd = new FlowDocument(); 11 var p = new Paragraph(); 12 p.FontSize = 16; 13 foreach (var txt in Model._report) 14 { 15 Section sec = new Section(); 16 p.Inlines.Add(txt.データ1); 17 18 LineBreak lb = new LineBreak(); 19 p.Inlines.Add(lb); 20 p.Inlines.Add(txt.データ2); 21 } 22 23 fd.Blocks.Add(p); 24 this.flowDoc.Document = fd;

または、

C#

1<Window x:Class="ReportLite3.Views.PrevReportWindow" 2 : 3 4 <Grid> 5 <FlowDocumentReader> 6 <FlowDocument> 7 <Paragraph FontFamily="Georgia" Background="Beige"> 8 <TextBlock Text="{Binding Path=Shindan}" Height="70" /> 9 </Paragraph> 10 11 <Paragraph FontFamily="Georgia" Background="Bisque"> 12 <TextBlock Text="{Binding Shoken}" Height="120" /> 13 </Paragraph> 14 </FlowDocument> 15 </FlowDocumentReader> 16 </Grid> 17</Window>

上記で表示のテストをしてみましたが、改ページする方法がわかりません。
教えてください。

Windows10 VS2015 C# WPF

追加

WPF

1 public partial class PrevReportWindow : Window 2 { 3 public string shindan; 4 public string shoken; 5 6 private void Window_Loaded(object sender, RoutedEventArgs e) 7 { 8 9 Model._report = (FlowDocumentReport)this.Resources["report"]; 10 Model._shindan = shindan; 11 Model._shoken = shoken; 12 Model.dispData(); 13 14 15 16 17 18 public class FlowDocumentReport : ObservableCollection<FlowDocumentModel> 19 { 20 21 } 22 23 24 public class PrevReport 25 { 26 public string shindan; // { get; set; } 27 public string shoken; // { get; set; } 28 } 29 30 public class FlowDocumentModel : INotifyPropertyChanged 31 { 32 public string _shindan; 33 public string _shoken; 34 public PrevReport report = new PrevReport(); 35 public FlowDocumentReport _report; 36 37 public event PropertyChangedEventHandler PropertyChanged; 38 public FlowDocumentModel() 39 { 40 PropertyChanged += (sender, e) => { }; 41 } 42 43 public void NotifyPropertyChanged(string propertyName) 44 { 45 if (PropertyChanged != null) 46 { 47 PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 48 } 49 } 50 51 public void dispData() 52 { 53 _report.Add(new FlowDocumentModel() 54 { 55 Shindan = GetHistoryData(HistoryShin, k), 56 Shoken = GetHistoryData(HistorySho, k) 57 }); 58 } 59 public string Shindan 60 { 61 get { return report.shindan; } 62 set 63 { 64 if (value != report.shindan) 65 { 66 report.shindan = value; 67 NotifyPropertyChanged("Shindan"); 68 } 69 } 70 } 71 public string Shoken 72 { 73 get { return report.shoken; } 74 set 75 { 76 if (value != report.shoken) 77 { 78 report.shoken = value; 79 NotifyPropertyChanged("Shoken"); 80 } 81 } 82 } 83}

この配列分ンページを作成したい
数分のページを作成したい

TN8001👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

どこで改ページしたいのかが伝わりませんが、次ページに送りたいブロック要素のBreakPageBeforetrueにすればいいんじゃないでしょうか。

Block.BreakPageBefore プロパティ (System.Windows.Documents) | Microsoft Docs

xml

1<FlowDocument> 2 <Paragraph Background="Beige" FontFamily="Georgia"> 3 <TextBlock Height="70" Text="{Binding Shindan}" /> 4 </Paragraph> 5 6 <Paragraph 7 Background="Bisque" 8 BreakPageBefore="True" 9 FontFamily="Georgia"> 10 <TextBlock Height="120" Text="{Binding Shoken}" /> 11 </Paragraph> 12</FlowDocument>

追記 コードビハインド

cs

1using System.Collections.Generic; 2using System.Windows; 3using System.Windows.Documents; 4 5namespace Questions320442 6{ 7 public partial class MainWindow : Window 8 { 9 private Model Model; 10 11 public MainWindow() 12 { 13 InitializeComponent(); 14 15 Model = new Model 16 { 17 _report = new List<Report> 18 { 19 new Report { データ1 = "page1", データ2 = "hogehoge", }, 20 new Report { データ1 = "page2", データ2 = "fugafuga", }, 21 // もちろん内容が長ければ途中で改ページが入るのでずれます 22 new Report { データ1 = "page3", データ2 = "piyo\npiyo\npiyo\npiyo\npiyo\npiyo\npiyo\npiyo\npiyo\npiyo\npiyo\npiyo\npiyo\npiyo\npiyo\npiyo\npiyo\npiyo\n", }, 23 } 24 }; 25 26 var flowDocument = new FlowDocument(); 27 foreach (var txt in Model._report) 28 { 29 // Paragraphもブロック要素 30 var paragraph = new Paragraph { FontSize = 16, BreakPageBefore = true, }; 31 paragraph.Inlines.Add(txt.データ1); 32 33 paragraph.Inlines.Add(new LineBreak()); 34 35 paragraph.Inlines.Add(txt.データ2); 36 flowDocument.Blocks.Add(paragraph); 37 } 38 39 flowDoc.Document = flowDocument; 40 } 41 } 42 43 class Model 44 { 45 public IEnumerable<Report> _report; 46 } 47 48 class Report 49 { 50 public string データ1; 51 public string データ2; 52 } 53}

投稿2021/02/04 09:31

編集2023/07/26 13:47
TN8001

総合スコア9396

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

byori

2021/02/05 00:39

お世話になります。 改ページのしたい形について説明不足ですみません。 this.DataContext = Model._report; の形でデータを渡したとき、Model._report 内の件数分ページとして作成したいです。 上記の質問の枠内にコードを追加しました。
TN8001

2021/02/05 03:06

だいぶ話が変わってきましたが、コードビハインドでModel._reportからFlowDocumentを作っていいのでしょうか? それとも「xamlでバインドできるか」という話ですか? できなくはないですが、コンバータを書いたりで面倒です。。。 INotifyPropertyChangedやINotifyCollectionChangedがありますが、文字や個数は動的に変わるんですか? INotifyPropertyChangedはRun.Textにバインドできますが、Collectionのほうは自前で入れ替えるしかないと思います。
byori

2021/02/05 03:27

お世話になります。 > 文字や個数は動的に変わるんですか? public class PrevReport { public string shindan; // { get; set; } public string shoken; // { get; set; } } 画像にある配列の中身は上記の2項目のみです。変わることはありません。 > 「xamlでバインドできるか」という話ですか? どういう形でも、表示ができればいいと考えています。 私に理解できる範囲になると思いますが・・・ > だいぶ話が変わってきましたが・・・ 多分、FlowDocument について私の知識不足や、思い込みがあったのかもしれません。
TN8001

2021/02/05 03:49

コードビハインド例を追記しました。 追加のコードを再現するのはちょっとめんどくさいので、当初のコードのほうでやらせていただきました^^;
byori

2021/02/06 03:28

TN8001 様、 サンプルのご提示ありがとうございます。 FlowDocument が少しづつわかったような気がします。 私の思い込みとFlowDocument の仕様がちょっとズレてましたけど何とかなりそうです。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問