wpf图表列系列数据绑定问题

发布于 2024-08-14 11:09:44 字数 1274 浏览 5 评论 0原文

我正在尝试使用 wpf 工具包中的 ColumnSeries 构建图表,但数据绑定似乎遇到问题。这是 xaml:

<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  

日期应该是独立值绑定,值应该是从属值绑定。

I'm trying to build a chart using the ColumnSeries from the wpf toolkit and I appear to be having trouble with the data binding. Here is the xaml:

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

In the code behind, I'm calling a stored procedure and putting the result in a DataTable like this:

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();
            }

No error is being generated but I'm not getting a column chart either. If I take the same command and put it into a DataGrid, it works fine. What do I need to do to get this into my column chart?

If it helps, the data comming back in the data tables looks like this:

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

The date should be the independent value binding and the value should be the dependent value binding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独难免 2024-08-21 11:09:44

这是我的工作代码,为了清晰起见,做了一些清理。也许这会有所帮助。

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;
                }

希望有帮助!

Here is my working code, cleaned up a little for clarity. Maybe this will help.

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>

code behind

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;
                }

Hope that helps!

秋叶绚丽 2024-08-21 11:09:44

我遇到了一个非常相似的问题,我有一个解决方案,它可能对你有用。

WPF 工具包图表和 IndependentValueBinding、IndependentValuePath

I'm having a very similiar problem, I have a bit of a solution, it would probably work for you.

WPF Toolkit Charting and IndependentValueBinding, IndependentValuePath

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文