0

在这里,我尝试分配数据源(使用示例应用程序中给出的相同代码)并创建一个图形,唯一的区别是我在 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); 
} 

谢克

4

1 回答 1

0

通过以下两项更改,您可以修复您的代码:

1 - 代替

Chart1.Series"Series 1".XValueMember = "Name"; 
Chart1.Series"Series 1".YValueMembers = "Sales";

Chart1.Series["Series 1"].XValueMember = "Name"; 
Chart1.Series["Series 1"].YValueMembers = "Sales";

2 - 在上述代码之前,插入以下行:

Chart1.ChartAreas.Add("Default");
Chart1.Series.Add(new Series("Series 1"));
于 2010-06-05T17:46:08.103 回答