PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

MigraDoc DocumentPreview window with MVVM in WPF
http://forum.pdfsharp.com/viewtopic.php?f=2&t=2475
Page 1 of 1

Author:  cymon [ Thu Jun 06, 2013 5:10 pm ]
Post subject:  MigraDoc DocumentPreview window with MVVM in WPF

I'm having trouble using the DocumentPreview in a WPF application. I'm trying to write it MVVM compliant (for unit testing purposes) which means, simply, binding important data to a datastructure behind it. However, when I do a line like:

Code:
<UserControl x:Class="ICS.TestStepView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:MigraDoc="clr-namespace:MigraDoc.Rendering.Windows;assembly=MigraDoc.Rendering-WPF">
...
<MigraDoc:DocumentPreview Ddl="{Binding BatchRecordDoc}"/>
...
</UserControl>

... then in runtime when the DocumentPreview is instantiated the error is thrown:
Code:
A 'Binding' cannot be set on the 'Ddl' property of type 'DocumentPreview'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.


What's everyone's preferred workaround on this one? Will there be a version of MagraDoc that fixes this?

Author:  blue666 [ Thu Oct 03, 2019 6:10 pm ]
Post subject:  Re: MigraDoc DocumentPreview window with MVVM in WPF

I know this is a really old question, but in case anybody is in need of an answer, here is what I do.
Code:
   public partial class ReportPreviewTab
    {
        public ReportPreviewTab() {
            InitializeComponent();
        }

        public bool DdlChanged { get => (bool)GetValue(DdlChangedProperty); set => SetValue(DdlChangedProperty, value); }

        public static readonly DependencyProperty DdlChangedProperty
            = DependencyProperty.Register(nameof(DdlChanged),
                                          typeof(bool),
                                          typeof(ReportPreviewTab),
                                          new PropertyMetadata(false));

        public string DdlString { get => (string)GetValue(DdlStringProperty); set => SetValue(DdlStringProperty, value); }

        public static readonly DependencyProperty DdlStringProperty
            = DependencyProperty.Register(nameof(DdlString),
                                          typeof(string),
                                          typeof(ReportPreviewTab),
                                          new PropertyMetadata(Paths.Instance.DefaultDdl, OnDdlChanged));

        private static void OnDdlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(d is ReportPreviewTab previewer)
            {
                try { previewer.docPreview.Ddl = e.NewValue?.ToString() ?? Paths.Instance.DefaultDdl; }
                catch(Exception ex)
                {
                    Log.Error(ex.Message, ex);
                    const string startDdl = @"\document{\section{";
                    const string middleDdl1 = @"\paragraph{The Report had an error!}";
                    const string middleDdl2 = @"\paragraph{Try Saving instead.}";
                    const string middleDdl3 = @"\paragraph{The error has been logged for the maintainer.}";

                    const string endDdl = @"}}";

                    previewer.docPreview.Ddl =$"{startDdl}{middleDdl1}{middleDdl2}{middleDdl3}{endDdl}";
                }
               
            }
        }


    }

Author:  blue666 [ Thu Oct 03, 2019 6:22 pm ]
Post subject:  Re: MigraDoc DocumentPreview window with MVVM in WPF

My answer was a little incomplete. Here is the xaml portion of my code behind. It is in a UserControl, you can name it whatever you want.
Code:
<UserControl x:Class="UserControls.ReportPreviewTab"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:migradoc="clr-namespace:MigraDoc.Rendering.Windows;assembly=MigraDoc.Rendering-wpf"
         xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300">
    <Frame>
        <Frame.Content>
            <Grid>
                <migradoc:DocumentPreview Name="docPreview"
                                          >
                    <migradoc:DocumentPreview.Resources>
                        <Style TargetType="ContentControl">
                            <Style.Triggers>
                                <Trigger Property="Name"
                                         Value="PART_FindToolBarHost">
                                    <Setter Property="Visibility"
                                            Value="Collapsed" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </migradoc:DocumentPreview.Resources>
                </migradoc:DocumentPreview>
            </Grid>
        </Frame.Content>
    </Frame>
</UserControl>


Then in another control (userControl, Window, etc.), I have this.
Code:

        <userControls:ReportPreviewTab Tag="Pdf Preview"
                                       Margin="10"
                                       Grid.Row="1"
                                       DdlString="{Binding PrintProperties.Ddl}" />



The PrintProperties.Ddl is a public string property in my ViewModel.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/