PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Mar 29, 2024 7:55 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jun 06, 2013 5:10 pm 
Offline

Joined: Wed Jun 05, 2013 9:33 pm
Posts: 1
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?


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 03, 2019 6:10 pm 
Offline

Joined: Wed May 09, 2012 10:06 pm
Posts: 5
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}";
                }
               
            }
        }


    }


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 03, 2019 6:22 pm 
Offline

Joined: Wed May 09, 2012 10:06 pm
Posts: 5
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 171 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group