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

DataTemplate Binding Data Print?

Hello, Sir

I would like to DataTemplate Binding print 
I had coded as follows
But, The binding data is not visible to the DataTemplate  
SfDataGrid.PrintSettings.PrintPageHeaderTemplate = Resources["PrintHeader"] as DataTemplate;
How do could be done with binding print the contents of the DataTemplate to print? 

xaml
<DataTemplate x:Key="PrintHeader"  >
            <Border BorderThickness="1">
                <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="50"></RowDefinition>
                    <RowDefinition Height="25"></RowDefinition>
                    <RowDefinition Height="25"></RowDefinition>
                    <RowDefinition Height="25"></RowDefinition>
                    <RowDefinition Height="50"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
                
                <TextBlock HorizontalAlignment="Center" FontSize="20" Text="TEST PRINT " Grid.ColumnSpan="4"/>
                    <TextBlock HorizontalAlignment="Right" Width="120" Grid.Row="1" FontSize="12" Text="NAME:" ></TextBlock>
                    <TextBlock HorizontalAlignment="Right" Width="120" Grid.Row="2" FontSize="12" Text="COMPANY:"></TextBlock>
                    <TextBlock HorizontalAlignment="Right" Width="120" Grid.Row="3" FontSize="12" Text="START TIME:"></TextBlock>
                    <TextBlock HorizontalAlignment="Right" Width="120" Grid.Row="4" FontSize="12" Text="END TIME:"></TextBlock>
                    <TextBlock Grid.Row="1"  Grid.Column="1" FontSize="12" HorizontalAlignment="Left" Text="{Binding Path= _Name, Mode=TwoWay}"/>
                    <TextBlock Grid.Row="2"  Grid.Column="1" FontSize="12" HorizontalAlignment="Left" Text="{Binding _CompanyName}" />
                    <TextBlock Grid.Row="3"  Grid.Column="1" FontSize="12" HorizontalAlignment="Left" Text="{Binding _StartTime}" />
                    <TextBlock Grid.Row="3"  Grid.Column="1" FontSize="12" HorizontalAlignment="Left" Text="{Binding _EndTime}" />
                </Grid>
            </Border>
        </DataTemplate>


       <ListBox Grid.Row="2" Width="400" Margin="10"  ItemsSource="{Binding PrintItemList}" ItemTemplate="{StaticResource PrintHeader}"   HorizontalContentAlignment="Stretch" Visibility="Collapsed" x:Name="printListView"/>


xaml.cs


private void PrintButton_Click(object sender, RoutedEventArgs e)
        {

            for (int i = 0; i < 1; i++)
            {
                PrintItemList.Add(new GradingRunPrintDetailItem() { _MemoryName = MemoryName.Text, _Company = "Company", _StartTime = TimeStart.Text, _EndTime = TimeEnd.Text, _FarmName = farmNameValue, _TotalCount = TotalSumWeight.Text, _TotalWeight = TotalSumCount.Text, _TotalAvgWeight = "0" });
            }

            if (SfDataGrid== null) return;
            string PrintMemoryName = MemoryName.Text;
            string FarmNameVal = FarmNameValue;
            string StartTimeVal = TimeStart.Text;
            string EndTimeVal = TimeEnd.Text;

            SfDataGrid.PrintSettings.AllowRepeatHeaders = false;
            SfDataGrid.PrintSettings.PrintPageHeaderHeight = 170;
            //SfDataGrid.PrintSettings.PrintManagerBase.PrintPageOrientation = PrintOrientation.Landscape;

            SfDataGrid.PrintSettings.PrintPageHeaderTemplate = Resources["PrintHeader"] as DataTemplate;
            SfDataGrid.ShowPrintPreview();
        } 


   SfDataGrid.PrintSettings.PrintPageHeaderTemplate = Resources["PrintHeader"] as DataTemplate;




1 Reply

JG Jai Ganesh S Syncfusion Team January 6, 2016 01:02 PM UTC

Hi Jenny,

We cannot understand your requirement clearly. We suspect that your reported problem is , the binding is not worked for some properties that is declared inside the DataTemplate . If this is your requirement then you can setting the DataContext like below code example,

Code Example:

<Window.Resources>

        <local:ViewModel x:Key="viewmodel"/>

        <DataTemplate x:Key="PrintHeader"  >

           

            <Border BorderThickness="1">

                <Grid DataContext="{StaticResource viewmodel}">

                    <Grid.RowDefinitions>

                        <RowDefinition Height="50"></RowDefinition>

                        <RowDefinition Height="25"></RowDefinition>

                        <RowDefinition Height="25"></RowDefinition>

                        <RowDefinition Height="25"></RowDefinition>

                        <RowDefinition Height="50"></RowDefinition>

                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>

                        <ColumnDefinition></ColumnDefinition>

                        <ColumnDefinition></ColumnDefinition>

                    </Grid.ColumnDefinitions>


                    <TextBlock HorizontalAlignment="Center" FontSize="20" Text="TEST PRINT " Grid.ColumnSpan="4"/>

                    <TextBlock HorizontalAlignment="Right" Width="120" Grid.Row="1" FontSize="12" Text="NAME:" ></TextBlock>

                    <TextBlock HorizontalAlignment="Right" Width="120" Grid.Row="2" FontSize="12" Text="COMPANY:"></TextBlock>

                    <TextBlock HorizontalAlignment="Right" Width="120" Grid.Row="3" FontSize="12" Text="START TIME:"></TextBlock>

                    <TextBlock HorizontalAlignment="Right" Width="120" Grid.Row="4" FontSize="12" Text="END TIME:"></TextBlock>

                    <TextBlock Grid.Row="1"  Grid.Column="1" FontSize="12" HorizontalAlignment="Left" Text="{Binding TemplateName}"/>

                   

                </Grid>

            </Border>

        </DataTemplate>


    </Window.Resources>


In other way, you can give a source when  binding data like below code example,


<TextBlock Grid.Row="1"  Grid.Column="1" FontSize="12" HorizontalAlignment="Left" Text="{Binding TemplateName,Source={StaticResource viewmodel}}"/>


If the above one is not your requirement, could you please share the more details for whether you want to perform the print for ListBox data or SfDataGrid? This would be more helpful for us to analyze further.

Regards,
Jai Ganesh S

Loader.
Live Chat Icon For mobile
Up arrow icon