我无法将数据绑定到 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);
}
}
数据的绑定没有渲染,但是标题和副标题渲染得很好,为什么?