WPF 中的 MS Chart,设置数据源不是创建系列
在这里,我尝试分配数据源(使用示例应用程序中给出的相同代码)并创建一个图表,唯一的区别是我在 WPF WindowsFormsHost 中执行此操作。由于某种原因,数据源未正确分配,我无法看到正在创建的系列(“系列 1”)。有线的事情是它可以在 Windows 窗体应用程序中工作,但不能在 WPF 应用程序中工作。
我错过了什么吗?有人可以帮助我吗?
谢谢
<Window x:Class="SEDC.MDM.WinUI.WindowsFormsHostWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:CHR="clr- namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.Dat aVisualization"
Title="HostingWfInWpf" Height="230" Width="338">
<Grid x:Name="grid1">
</Grid>
</Window>
private void drawChartDataBinding()
{
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
string fileNameString = @"C:\Users\Shaik\MSChart\WinSamples\WinSamples\data\chartdata.mdb";
// initialize a connection string
string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString;
// define the database query
string mySelectQuery = "SELECT * FROM REPS;";
// create a database connection object using the connection string
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
// create a database command on the connection using query
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
Chart Chart1 = new Chart();
// set chart data source
Chart1.DataSource = myCommand;
// set series members names for the X and Y values
Chart1.Series"Series 1".XValueMember = "Name";
Chart1.Series"Series 1".YValueMembers = "Sales";
// data bind to the selected data source
Chart1.DataBind();
myCommand.Dispose();
myConnection.Close();
host.Child = Chart1;
this.grid1.Children.Add(host);
}
谢克
Here I am trying to assign the datasource (using same code given in the sample application) and create a graph, only difference is i am doing it in WPF WindowsFormsHost. due to some reason the datasource is not being assigned properly and i am not able to see the series ("Series 1") being created. wired thing is that it is working in the Windows Forms application but not in the WPF one.
am i missing something and can somebody help me?
Thanks
<Window x:Class="SEDC.MDM.WinUI.WindowsFormsHostWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:CHR="clr- namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.Dat aVisualization"
Title="HostingWfInWpf" Height="230" Width="338">
<Grid x:Name="grid1">
</Grid>
</Window>
private void drawChartDataBinding()
{
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
string fileNameString = @"C:\Users\Shaik\MSChart\WinSamples\WinSamples\data\chartdata.mdb";
// initialize a connection string
string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString;
// define the database query
string mySelectQuery = "SELECT * FROM REPS;";
// create a database connection object using the connection string
OleDbConnection myConnection = new OleDbConnection(myConnectionString);
// create a database command on the connection using query
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
Chart Chart1 = new Chart();
// set chart data source
Chart1.DataSource = myCommand;
// set series members names for the X and Y values
Chart1.Series"Series 1".XValueMember = "Name";
Chart1.Series"Series 1".YValueMembers = "Sales";
// data bind to the selected data source
Chart1.DataBind();
myCommand.Dispose();
myConnection.Close();
host.Child = Chart1;
this.grid1.Children.Add(host);
}
Shaik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过以下两项更改,您可以修复代码:
1 - 而不是
编写
2 - 在上述代码之前,插入以下行:
With the following two changes, you can fix your code:
1 - Instead of
write
2 - Before the above code, insert the following lines: