0

我无法将数据绑定到 Modern UI Chart 控件。

我没有以创建 XAML 元素然后绑定到该元素的常见方式使用它,我的目标是创建元素并将其呈现在内存中,然后获取元素的图像。

我下面的代码用于创建和添加内容,图像的渲染效果很好。

    void generateGraphImg()
    {
        PieChart pc = new PieChart();
        ObservableCollection<TestClass>  Errors = new ObservableCollection<TestClass>();
        Errors.Add(new TestClass() { Category = "Globalization", Number = 75 });
        Errors.Add(new TestClass() { Category = "Features", Number = 2 });
        Errors.Add(new TestClass() { Category = "ContentTypes", Number = 12 });
        Errors.Add(new TestClass() { Category = "Correctness", Number = 83 });
        Errors.Add(new TestClass() { Category = "Best Practices", Number = 29 });

        ChartSeries Charts = new ChartSeries();
        Charts.SeriesTitle = "Errors";
        Charts.DisplayMember = "Category";
        Charts.ValueMember = "Number";
        Charts.ItemsSource = Errors;
        pc.Series.Add(Charts);

        pc.ChartTitle = "Minimal Pie Chart";
        pc.ChartSubTitle = "Chart with fixed width and height";

        makeImgFromControl(pc, @"C:\Modern UI IMG\img.bmp");
    }
    void makeImgFromControl(UIElement control, string saveTo)
    {
        Viewbox viewbox = new Viewbox();
        viewbox.Child = control; //control to render
        viewbox.Measure(new System.Windows.Size(200, 200));
        viewbox.Arrange(new Rect(0, 0, 200, 200));
        viewbox.UpdateLayout(); //On PiePiece DrawGeometry() happens here
        RenderTargetBitmap render = new RenderTargetBitmap(500,500, 150, 150, PixelFormats.Pbgra32);
        render.Render(viewbox);
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(render));
        using (Stream s = File.Create(saveTo))
        {
            encoder.Save(s);
        }
    }

数据的绑定没有渲染,但是标题和副标题渲染得很好,为什么?

4

2 回答 2

1

解决方案隐藏在 LayoutUpdated 中,经过一段时间破解 Modern UI Chart 的代码后,我才恍然大悟。我需要等一下才能拿到我的img。

    void generateGraphImg()
    {
        PieChart pc = new PieChart();
        pc.LayoutUpdated += pc_LayoutUpdated;
        ObservableCollection<TestClass> Errors = new ObservableCollection<TestClass>();
        Errors.Add(new TestClass() { Category = "Globalization", Number = 75 });
        Errors.Add(new TestClass() { Category = "Features", Number = 2 });
        Errors.Add(new TestClass() { Category = "ContentTypes", Number = 12 });
        Errors.Add(new TestClass() { Category = "Correctness", Number = 83 });
        Errors.Add(new TestClass() { Category = "Best Practices", Number = 29 });

        ChartSeries Charts = new ChartSeries();
        Charts.SeriesTitle = "Errors";
        Charts.DisplayMember = "Category";
        Charts.ValueMember = "Number";
        Charts.ItemsSource = Errors;
        pc.Series.Add(Charts);

        pc.ChartTitle = "Minimal Pie Chart";
        pc.ChartSubTitle = "Chart with fixed width and height";

        makeImgFromControl(pc, @"C:\Modern UI IMG\img.bmp");
    }

    void pc_LayoutUpdated(object sender, EventArgs e)
    {
        RenderTargetBitmap render = new RenderTargetBitmap(500,500, 150, 150, PixelFormats.Pbgra32);
        render.Render(viewbox);
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(render));
        using (Stream s = File.Create(@"C:\Modern UI IMG\img.bmp"))
        {
            encoder.Save(s);
        }
    }

    Viewbox viewbox;
    void makeImgFromControl(UIElement control, string saveTo)
    {
        viewbox = new Viewbox();
        viewbox.Child = control; //control to render
        viewbox.Measure(new System.Windows.Size(200, 200));
        viewbox.Arrange(new Rect(0, 0, 200, 200));
        viewbox.UpdateLayout();
    }
于 2014-09-22T12:45:22.860 回答
0

Try assigning the ItemSource after having added the ChartSeries. So change the order of:

Charts.ItemsSource = Errors;
pc.Series.Add(Charts);

to:

pc.Series.Add(Charts);
Charts.ItemsSource = Errors;

I tested and this seems to make a difference. Not exactly sure why, but it is possible the chart object only updates on receiving collection change notifications.


UPDATE: looking at the source code, it seems the pie chart is composed of PiePiece controls, which has in its constructor:

public PiePiece()
{
    ...
    Loaded += PiePiece_Loaded;
}

void PiePiece_Loaded(object sender, RoutedEventArgs e)
{
      DrawGeometry();
}

so it seems to only draw itself when actually Loaded.

于 2014-09-17T12:00:09.357 回答