如何以编程方式绑定(DataSet to Chart)?

wpf binding graph dataset

2145 观看

3回复

474 作者的声誉

任何人都可以在此代码中识别问题吗?(我在WPF绑定中有点新手。)当我点击按钮加载图表后执行此代码:我收到此错误:

更新:我不再得到那个错误。感谢托马斯。

现在没有错误发生但图表看起来完全空白(没有列)

更新:代码现在看起来像这样:

            // create a very simple DataSet
        var dataSet = new DataSet("MyDataSet");
        var table = dataSet.Tables.Add("MyTable");
        table.Columns.Add("Name");
        table.Columns.Add("Price");
        table.Rows.Add("Brick", 1.5d);
        table.Rows.Add("Soap", 4.99d);
        table.Rows.Add("Comic Book", 0.99d);

        // chart series
        var series = new ColumnSeries()
        {
            IndependentValueBinding = new Binding("[Name]"), // How to deal with
            DependentValueBinding = new Binding("[Price]"), //  these two?
            ItemsSource = dataSet.Tables[0].DefaultView // or maybe I do mistake here?
        };

        // ---------- set additional binding as adviced ------------------
        series.SetBinding(ColumnSeries.ItemsSourceProperty, new Binding());

        // chart stuff
        MyChart.Series.Add(series);
        MyChart.Title = "Names 'n Prices";

        // some code to remove legend
        var style = new Style(typeof(Control));
        style.Setters.Add(new Setter(LegendItem.TemplateProperty, null));
        MyChart.LegendStyle = style;

XAML:

<Window x:Class="BindingzTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="606" Width="988" xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
<Grid Name="LayoutRoot">
    <charting:Chart Name="MyChart" Margin="0,0,573,0" Height="289" VerticalAlignment="Top" />
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="272,361,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="chart1_Loaded" />
</Grid>
</Window>
作者: user175908 的来源 发布者: 2011 年 1 月 1 日

回应 (3)


1

5460 作者的声誉

看来你必须调用这里提到的SetBinding方法http://forums.silverlight.net/forums/p/151817/350809.aspx

作者: Tomas Voracek 发布者: 01.01.2011 07:10

1

474 作者的声誉

我有点解决了......

        // create a very simple DataSet
        var dataSet = new DataSet("MyDataSet");
        var table = dataSet.Tables.Add("MyTable");
        table.Columns.Add("Name");
        table.Columns.Add("Price");
        table.Rows.Add("Brick", 1.5d);
        table.Rows.Add("Soap", 4.99d);
        table.Rows.Add("Comic Book", 0.99d);
        table.Rows.Add("Broken Table", 10.99d);

        // chart series
        var series = new ColumnSeries()
        {             
            IndependentValueBinding = new Binding("Key"),
            DependentValueBinding = new Binding("Value"), 
            ItemsSource = from x in dataSet.Tables[0].AsEnumerable()
                          select new KeyValuePair<string, double>((string)x["Name"], double.Parse((string)x["Price"]))
        };

        // chart stuff       
        MyChart.Series.Add(series); 
        MyChart.Title = "Names 'n Prices"; 

        // some code to remove legend    
        var style = new Style(typeof(Control)); 
        style.Setters.Add(new Setter(LegendItem.TemplateProperty, null));   
        MyChart.LegendStyle = style; 

那个SetBinding()东西只让图表看起来空白......

LINQ确实拥有,但它增加了不必要的复杂性。必须有办法做到这一点没有linq ...虽然我对结果很满意。目前。

谢谢大家。

作者: user175908 发布者: 01.01.2011 08:42

1

474 作者的声誉

使用值转换器找到了更好的方法:PS:我得到了异常,因为存储在表中的对象通过defaut 转换为字符串。图表需要数量 ......所以我写了一个ValueConverter,让一切正常。

    // create a very simple DataSet
    var dataSet = new DataSet("MyDataSet");
    var table = dataSet.Tables.Add("MyTable");
    table.Columns.Add("Name");         
    table.Columns.Add("Price");
    table.Rows.Add("Brick", 1.5d);
    table.Rows.Add("Soap", 4.99d);   
    table.Rows.Add("Comic Book", 0.99d);   
    table.Rows.Add("Broken Table", 10.99d);  
    // chart series         
    var series = new ColumnSeries()     
    {                          
        IndependentValueBinding = new Binding("Key"), 
        DependentValueBinding = new Binding("Value")
        {
             Converter = new Conv()
        },
        ItemsSource = dataSet.Tables["MyTable"]; 

    };          
    // chart stuff
    MyChart.Series.Add(series);
    MyChart.Title = "Names 'n Prices";
    // some code to remove legend     
    var style = new Style(typeof(Control)); 
    style.Setters.Add(new Setter(LegendItem.TemplateProperty, null));                
    MyChart.LegendStyle = style;  

...

 public class Conv : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return System.Convert.ToInt32(value.ToString());
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value.ToString();
    }
}
作者: user175908 发布者: 02.01.2011 01:00
32x32