How to do binding programmically?

Posted by user175908 on Stack Overflow See other posts from Stack Overflow or by user175908
Published on 2011-01-01T18:59:25Z Indexed on 2011/01/01 20:54 UTC
Read the original article Hit count: 261

Filed under:
|
|
|

Hello,

Could anyone identify the problem in this code? (I'm kinda newbie in WPF bindings.) This code executes after chart is loaded when I click a button: I get this error:

Update: I dont get that error anymore. Thanks to Tomas.

Now no error occur but chart looks completely blank (no columns)

Update: Code now looks like this:

            // 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>

Thanks for help in advance once more.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about binding