WPF图表打印到XPS

时间:2012-09-06 13:41:59

标签: wpf charts xps

更新:我通过为系列数据点创建简单的样式模板来修复此问题。对于任何面临类似问题的人来说,这是我简单的代码。此代码不显示LegendItems系列,但我认为它可以用类似的方式修复。

<Style
            x:Key="MyColumnDataPointStyle"
            TargetType="charting:ColumnDataPoint">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="charting:ColumnDataPoint">
                    <Border
                            Background="Plum"
                            BorderBrush="Gray"
                            BorderThickness="1"
                            Opacity="1"
                            x:Name="Root" >

                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="Root" Property="Background" Value="Yellow"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我在WPF / C#项目中使用system.windows.controls.datavisualization.toolkit来创建图表。

我创建图表没有问题,但是当我尝试将其打印到XPS时,它根本不会显示系列。加载窗口时打印图表工作正常但我需要打印图表而不在窗口中显示它。

窗口中显示的图表:

Normal chart

XPS中的图表:

XPS chart

以下是我创建XPS文件的方法:

 XpsDocument doc = new XpsDocument(@".\chart.xps", FileAccess.Write);

 XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);

 VisualsToXpsDocument vToXpsD = (VisualsToXpsDocument)writer.CreateVisualsCollator();

 foreach (Canvas canvas in reportViewer.Document.Pages)
 {
          canvas.Measure(canvas.RenderSize);

          canvas.Arrange(new Rect(new Point(0, 0), canvas.RenderSize));

          vToXpsD.Write(canvas);   
  }

  vToXpsD.EndBatchWrite();

  doc.Close();

图表代码:

Chart chart = new Chart();

BarSeries bar = new BarSeries();

Binding valuesBinding = new Binding("Value");
Binding namesBinding = new Binding("Title");

bar.ItemsSource = content.Series[0].Points;
bar.IndependentValueBinding = namesBinding;
bar.DependentValueBinding = valuesBinding; 

chart.Series.Add(bar);

我试过这个solution,但我无法让它发挥作用。

非常感谢任何帮助!

0 个答案:

没有答案