Back to Devexpress

CollectionViewLink Class

wpf-devexpress-dot-xpf-dot-printing-129d5d2c.md

latest6.9 KB
Original Source

CollectionViewLink Class

A link to print objects which implement the ICollectionView interface.

Namespace : DevExpress.Xpf.Printing

Assembly : DevExpress.Xpf.Printing.v25.2.dll

NuGet Package : DevExpress.Wpf.Printing

Declaration

csharp
public class CollectionViewLink :
    TemplatedLink
vb
Public Class CollectionViewLink
    Inherits TemplatedLink

Remarks

If the CollectionViewLink.CollectionView property is not set, calling the LinkBase.CreateDocument method fails with an exception.

Example

The following example demonstrates how to use the CollectionViewLink class to print data from a hierarchical datasource, which implements the ICollectionView interface.

For this, it is necessary to do the following:

View Example

csharp
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using DevExpress.Xpf.Printing;
// ...
private void button1_Click(object sender, RoutedEventArgs e) {
    // Create a link and bind it to the PrintPreview instance.
    CollectionViewLink link = new CollectionViewLink();
    //preview.Model = new LinkPreviewModel(link);

    // Create an ICollectionView object.
    link.CollectionView = CreateMonthCollectionView();

    // Provide export templates.
    link.DetailTemplate = (DataTemplate)Resources["monthNameTemplate"];
    link.GroupInfos.Add(new GroupInfo((DataTemplate)Resources["monthQuarterTemplate"]));

    // Create a document.
    link.CreateDocument(true);

    // Show a Print Preview.
    PrintHelper.ShowPrintPreviewDialog(this, link);
}

private ICollectionView CreateMonthCollectionView() {
    const int monthCount = 12;
    string[] monthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames;

    MonthItem[] data = new MonthItem[Math.Min(monthNames.Length, monthCount)];
    for (int i = 0; i < data.Length; i++) {
        data[i] = new MonthItem(monthNames[i], (i / 3) + 1);
    }

    CollectionViewSource source = new CollectionViewSource();
    source.Source = data;
    source.GroupDescriptions.Add(new PropertyGroupDescription("Quarter"));

    return source.View;
}

public class MonthItem {
    public string Name { get; private set; }
    public int Quarter { get; private set; }

    public MonthItem(string name, int quarter) {
        Name = name;
        Quarter = quarter;
    }
}
xaml
<Window.Resources>
    <DataTemplate x:Key="monthNameTemplate">
        <dxe:TextEdit Text="{Binding Path=Content.Name, Mode=OneWay}" />
    </DataTemplate>
    <DataTemplate x:Key="monthQuarterTemplate">
        <dxe:TextEdit Text="{Binding Path=Content.Name, Mode=OneWay}" FontWeight="Bold" />
    </DataTemplate>
</Window.Resources>
vb
Imports System
Imports System.ComponentModel
Imports System.Globalization
Imports System.Windows
Imports System.Windows.Data
Imports DevExpress.Xpf.Printing
' ...
Private Sub button1_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    ' Create a link and bind it to the PrintPreview instance.
    Dim link As New CollectionViewLink()
    'preview.Model = new LinkPreviewModel(link);

    ' Create an ICollectionView object.
    link.CollectionView = CreateMonthCollectionView()

    ' Provide export templates.
    link.DetailTemplate = DirectCast(Resources("monthNameTemplate"), DataTemplate)
    link.GroupInfos.Add(New GroupInfo(DirectCast(Resources("monthQuarterTemplate"), DataTemplate)))

    ' Create a document.
    link.CreateDocument(True)

    ' Show a Print Preview.
    PrintHelper.ShowPrintPreviewDialog(Me, link)
End Sub

Private Function CreateMonthCollectionView() As ICollectionView
    Const monthCount As Integer = 12
    Dim monthNames() As String = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames

    Dim data(Math.Min(monthNames.Length, monthCount) - 1) As MonthItem
    For i As Integer = 0 To data.Length - 1
        data(i) = New MonthItem(monthNames(i), (i \ 3) + 1)
    Next i

    Dim source As New CollectionViewSource()
    source.Source = data
    source.GroupDescriptions.Add(New PropertyGroupDescription("Quarter"))

    Return source.View
End Function

Public Class MonthItem
    Private privateName As String
    Public Property Name() As String
        Get
            Return privateName
        End Get
        Private Set(ByVal value As String)
            privateName = value
        End Set
    End Property
    Private privateQuarter As Integer
    Public Property Quarter() As Integer
        Get
            Return privateQuarter
        End Get
        Private Set(ByVal value As Integer)
            privateQuarter = value
        End Set
    End Property

    Public Sub New(ByVal name As String, ByVal quarter As Integer)
        Me.Name = name
        Me.Quarter = quarter
    End Sub
End Class

Inheritance

Object DispatcherObject DependencyObject LinkBase TemplatedLink CollectionViewLink

See Also

CollectionViewLink Members

DevExpress.Xpf.Printing Namespace