Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

c# - Adding dynamically flow document to XAML using MVVM pattern

I know this is not the place to ask this kind of questions , but is possible to add a FlowDocument created in a View Model to XAML? I tried to create a FlowDocument in XAML and then bind to a FlowDocument property of ViewModel but not working. I will post the implementation below:

  <FlowDocumentScrollViewer>
            <FlowDocument DataContext="{Binding FlowDocument, Mode=OneWay}"></FlowDocument>
        </FlowDocumentScrollViewer>
    </StackPanel>

Here is the property created in View model :

 private FlowDocument _flowDoc = new FlowDocument();
  public FlowDocument FlowDocument
        {
            get => _flowDoc;

            set
            {               
                SetProperty(ref _flowDoc, value);

            }
        }

And here is how I create the flow doc :

 public void Execute(object parameter)
    {
        DataGrid dataGrid = (DataGrid)parameter;

            FlowDocument fd = new FlowDocument();

            Paragraph p = new Paragraph(new Run("Timesheet"));
            p.FontStyle = dataGrid.FontStyle;
            p.FontFamily = dataGrid.FontFamily;
            p.FontSize = 18;
            fd.Blocks.Add(p);

            Table table = new Table();
            TableRowGroup tableRowGroup = new TableRowGroup();
            TableRow r = new TableRow();

            fd.BringIntoView();

            fd.TextAlignment = TextAlignment.Center;
            fd.ColumnWidth = 500;
            table.CellSpacing = 0;

            var headerList = dataGrid.Columns.Select(e => e.Header.ToString()).ToList();


            for (int j = 0; j < headerList.Count; j++)
            {

                r.Cells.Add(new TableCell(new Paragraph(new Run(headerList[j]))));
                r.Cells[j].ColumnSpan = 4;
                r.Cells[j].Padding = new Thickness(4);

                r.Cells[j].BorderBrush = Brushes.Black;
                r.Cells[j].FontWeight = FontWeights.Bold;
                r.Cells[j].Background = Brushes.DarkGray;
                r.Cells[j].Foreground = Brushes.White;
                r.Cells[j].BorderThickness = new Thickness(1, 1, 1, 1);
            }
            tableRowGroup.Rows.Add(r);
            table.RowGroups.Add(tableRowGroup);
            foreach(TimeSheetRow row in dataGrid.ItemsSource)
            { 

                table.BorderBrush = Brushes.Gray;
                table.BorderThickness = new Thickness(1, 1, 0, 0);
                table.FontStyle = dataGrid.FontStyle;
                table.FontFamily = dataGrid.FontFamily;
                table.FontSize = 13;
                tableRowGroup = new TableRowGroup();
                r = new TableRow();
            int j = 0;
                foreach(var property in row.GetType().GetProperties())
                { 
                if(property != null)
                {

                    r.Cells.Add(new TableCell(new Paragraph(new Run(property.GetValue(row).ToString()))));
                    r.Cells[j].ColumnSpan = 1;
                    r.Cells[j].Padding = new Thickness(1);

                    r.Cells[j].BorderBrush = Brushes.DarkGray;
                    r.Cells[j].BorderThickness = new Thickness(0, 0, 1, 1);
                }

                ++j;
                }

                tableRowGroup.Rows.Add(r);
                table.RowGroups.Add(tableRowGroup);
            j = 0;
            }
            fd.Blocks.Add(table);

        TViewModel.FlowDocument = fd;


       // }

    }

The method Execute is part of a class that implements ICommand interface , and TViewModel is the View Model corresponding to the View I am trying to add the flow document .
Note: Please don't take into consideration coding skills as this code was copied from internet and I am trying to get used with flow documents

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Document is a dependency property which you can bind to a FlowDocument property:

<FlowDocumentScrollViewer Document="{Binding FlowDocument}" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...