【问题标题】:How do I bind my LINQ query results (put into a dictionary) to a Chart in WPF?如何将我的 LINQ 查询结果(放入字典)绑定到 WPF 中的图表?
【发布时间】:2011-12-27 20:14:46
【问题描述】:

好的,所以我只是在练习 wpf 中的图表可能是一些非常基本的东西。我对 linq 和 wpf 相当陌生,所以如果这是一个愚蠢的问题,我提前道歉。我有我的 LINQ to entity 查询返回所有列,我想选择其中两列(月(字符串),点(整数))将它们放入字典中,然后将其绑定到我的图表..

这是我的 XAML::

<Window x:Class="Chart.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DVC:Chart Name="testchart" Title="Statistics" Width="500" Height="300" LegendTitle="Stats">
        <DVC:Chart.Series>
            <DVC:BarSeries Title="Points"
                           IndependentValueBinding=""
                           DependentValueBinding="">
            </DVC:BarSeries>
        </DVC:Chart.Series>
    </DVC:Chart>
</Grid>

下面是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;

namespace Chart
{

public partial class MainWindow : Window
{
    Dictionary<string, int> _data = new Dictionary<string, int>();

    public MainWindow()
    {
        InitializeComponent();
        using (CE ctx = new CE())
        {
            var query = (from r in ctx.ChartOnes
                           select r).ToDictionary(k => k.Month);
        }


    }

}
}

感谢 Dessus 的及时回复!我今天会玩弄你的建议。昨天我确实从一位同事那里得到了一些帮助,他能够向我展示如何让它发挥作用。这是编辑后的代码sn-ps:

        <DVC:Chart Name="testchart" Title="Statistics" Width="500" Height="300" LegendTitle="Stats">
        <DVC:Chart.Series>
                <DVC:ColumnSeries Title="Points"
                           IndependentValueBinding="{Binding Path=Key}"
                           DependentValueBinding="{Binding Path=Value.Points}">
                </DVC:ColumnSeries>
            </DVC:Chart.Series>
    </DVC:Chart>

和代码隐藏:

        private void LoadBarChartData()
    {
        CE ctx = new CE();

        var query = (from r in ctx.ChartOnes
                     select new { Points = r.Points, Month = r.Month }).ToDictionary(k => k.Month);

        ((ColumnSeries)testchart.Series[0]).ItemsSource  = query;
    }

【问题讨论】:

    标签: c# wpf linq dictionary charts


    【解决方案1】:

    您应该查找一个模型视图视图模型 (MVVM) 示例,了解如何在 WPF 中为图表控件执行数据绑定(请参阅:http://geekswithblogs.net/BillSontag/archive/2009/10/31/wpf-2-way-data-binding.aspx)。您在后面的代码中进行查询的方式会使获取对您的控件的引用变得烦人(但可以工作),但是一旦您开始工作并习惯它,MVVM 将使生活变得更轻松。您的 Linq 查询看起来应该输出到数据表而不是字典。

    旁注:对基础 LINQ 集合的任何更新都不会触发图表控件中的更新。为此,您需要类似:http://bindablelinq.codeplex.com/

    【讨论】:

    • 谢谢! MVVM 示例非常简单易懂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多