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. Image for the cookie policy date

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

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.


1 Reply

SR Sivakumar R Syncfusion Team May 26, 2016 04:57 PM UTC

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 


Loader.
Live Chat Icon For mobile
Up arrow icon