We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

How to bind a property to a ModelView DataContext when setting Header for printing ?

Thread ID:

Created:

Updated:

Platform:

Replies:

124231 May 25,2016 11:59 AM May 26,2016 12:57 PM WPF 1
loading
Tags: SfDataGrid
Silvio
Asked On May 25, 2016 11:59 AM

HI,

I'm following your examples to customize Header for SfDatagrid printing.

In the XAML I have this code:


<Application.Resources>
    <DataTemplate x:Key="PageHeaderTempalte">
        <Grid Background="Gray">
            <TextBlock Text="{Binding ReportTitle}" 
                       FontSize="18" 
                       FontWeight="Bold" 
                       Foreground="White" 
                       HorizontalAlignment="Center"/>
        </Grid>
    </DataTemplate>
</Application.Resources>


where "ReportTitle" is a string property that supplies the report title to be set for the report that's available on my ViewModel object.
But, how can I set the DataContext for the PrintPreview window in order to have this binding work ?

Best regards.


Sivakumar R [Syncfusion]
Replied On May 26, 2016 12:57 PM

Hi Silvio, 
 

PrintManagerBase is the DataContext for PrintPageControl, where the header and footer templates are loaded. Where you can override print manager and pass DataContext to Header and Footer templates. You can find the sample and code snippet details below, 

public class GridPrintManagerExt: GridPrintManager 
{ 
    public GridPrintManagerExt(SfDataGrid datagrid) : base(datagrid) 
    { 
             
    } 
 
    SfDataGrid _dataGrid; 
    public SfDataGrid DataGrid 
    { 
        get { return dataGrid; } 
        set { _dataGrid = value; } 
    } 
} 
 
dataGrid.PrintSettings.PrintManagerBase = new GridPrintManagerExt(dataGrid); 
dataGrid.PrintSettings.PrintPageHeaderTemplate = App.Current.Resources["PageHeaderTempalte"] as DataTemplate; 
dataGrid.PrintSettings.PrintPageHeaderHeight = 30; 
dataGrid.ShowPrintPreview(); 
 
XAML 
<Application.Resources> 
    <DataTemplate x:Key="PageHeaderTempalte"> 
        <Grid Background="Gray"> 
            <TextBlock  
                    FontSize="18"  
                    FontWeight="Bold"  
                    Foreground="White"  
                    HorizontalAlignment="Center"> 
                <TextBlock.Text> 
                    <Binding Path="DataContext.DataGrid.DataContext.ReportTitle" 
                            RelativeSource="{RelativeSource Mode=FindAncestor, 
                                                            AncestorType={x:Type syncfusion:PrintPageControl}}" 
                            StringFormat="-- {0} --" /> 
                </TextBlock.Text> 
            </TextBlock> 
        </Grid> 
    </DataTemplate> 
</Application.Resources> 
 
Sample: 
 
UG link: 
 
Thanks, 
Sivakumar 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;