2009-12-12 66 views
1

我想從wpf工具包中使用ColumnSeries生成圖表,我似乎遇到了數據綁定問題。這裏是XAML:wpf製圖列系列數據綁定問題

<Grid> 
    <chartingToolkit:ColumnSeries Height="18" HorizontalAlignment="Left" Margin="188,169,0,0" Name="columnSeries1" VerticalAlignment="Top" Width="18" IndependentValueBinding="{Binding Path=Date}" DependentValueBinding="{Binding Path=Value}" /> 
</Grid> 

在後面的代碼,我調用存儲過程,並把結果在DataTable這樣的:

string connString = ConfigurationManager.ConnectionStrings["string"].ConnectionString; 
      using (SqlConnection cn = new SqlConnection(connString)) 
      { 
       DataTable dt = new DataTable("T1"); 
       cn.Open(); 
       SqlCommand cmd = new SqlCommand("T1_sp", cn); 
       cmd.CommandType = CommandType.StoredProcedure; 
       SqlDataAdapter da = new SqlDataAdapter(cmd); 
       da.Fill(dt); 
       columnSeries1.ItemsSource = dt.DefaultView; 
       cn.Close(); 
      } 

正在生成沒有錯誤,但我沒有得到柱形圖。如果我採用相同的命令並將其放入DataGrid,它可以正常工作。我需要做些什麼才能將其添加到我的柱形圖中?

如果有幫助,該數據在數據表中回正在添加如下:

Date, type, Value 
2009-10-09, abc, 12.23 
2009-10-10, def, 13.35 

日期應爲獨立值綁定和值應爲相關值綁定。

回答

2

這是我的工作代碼,爲了清晰起見,清理了一下。也許這會有所幫助。

XAML

<charting:Chart Grid.Column="1" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Transparent" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"> 
     <charting:Chart.Series> 
      <charting:ColumnSeries x:Name="TrendChart" ItemsSource="{Binding Trend1}" IndependentValueBinding="{Binding Date}" DependentValueBinding="{Binding Spread}"> 
      </charting:ColumnSeries> 
     </charting:Chart.Series>    
    </charting:Chart> 

代碼背後

string connString = ConfigurationManager.ConnectionStrings["string"].ConnectionString; 
       using (SqlConnection cn = new SqlConnection(connString)) 
       { 
        DataTable dt = new DataTable("Trend1"); 
        cn.Open(); 
        SqlCommand cmd = new SqlCommand("Trend1_sp", cn);      cmd.CommandType = CommandType.StoredProcedure; 
        SqlDataAdapter da = new SqlDataAdapter(cmd); 
        da.Fill(dt); 
        TrendChart.ItemsSource = dt.DefaultView; 
       } 

希望幫助!