Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF项目的UserControl中使用winformchart_C#_Wpf_Winforms_User Controls - Fatal编程技术网

C# 在WPF项目的UserControl中使用winformchart

C# 在WPF项目的UserControl中使用winformchart,c#,wpf,winforms,user-controls,C#,Wpf,Winforms,User Controls,我正在尝试在UserControl中显示图表。在窗口中进行此操作时,效果良好 xaml代码: <WindowsFormsHost x:Name="host" Height="300" Width="300"> <winformchart:Chart x:Name="MyWinformChart"> <winformchart:Chart.Series> <winformchart:Series Name="series" Char

我正在尝试在UserControl中显示图表。在窗口中进行此操作时,效果良好

xaml代码:

<WindowsFormsHost x:Name="host" Height="300" Width="300">
  <winformchart:Chart x:Name="MyWinformChart">
    <winformchart:Chart.Series>
      <winformchart:Series Name="series" ChartType="Line"/>
    </winformchart:Chart.Series>
    <winformchart:Chart.ChartAreas>
      <winformchart:ChartArea/>
    </winformchart:Chart.ChartAreas>
  </winformchart:Chart>
</WindowsFormsHost>

以及背后的代码:

using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Forms.DataVisualization.Charting;

namespace DragonLink.View {
    public partial class TXSpectrumAnalyzer : UserControl {
        Dictionary<int, double> value;
        public TXSpectrumAnalyzer() {
            InitializeComponent();

            value = new Dictionary<int, double>();
            for (int i = 0; i < 10; i++)
                value.Add(i, 10 * i);

             Chart chart = this.FindName("MyWinformChart") as Chart;
             chart.ChartAreas.Add("Default");
             chart.DataSource = value;
             chart.Series["series"].XValueMember = "Key";
             chart.Series["series"].YValueMembers = "Value";
        }
    }
}
使用System.Collections.Generic;
使用System.Windows.Controls;
使用System.Windows.Forms.DataVisualization.Charting;
名称空间DragonLink.View{
公共部分类TXSpectrumAnalyzer:UserControl{
字典值;
公共TXSpectrumAnalyzer(){
初始化组件();
value=新字典();
对于(int i=0;i<10;i++)
增值(i,10*i);
Chart Chart=此.FindName(“MyWinformChart”)作为图表;
chart.ChartAreas.Add(“默认”);
chart.DataSource=值;
图表.Series[“Series”].XValueMember=“Key”;
图表.Series[“Series”].YValueMembers=“Value”;
}
}
}
在窗口中使用此代码,效果很好。 有人能告诉我为什么这在用户控件中不起作用,或者以其他方式显示winForm图表吗


感谢您的帮助。

解决了这个问题。由于某些原因,当窗口的属性AllowsTransparency设置为True时,图表不会显示。改变这个错误,它就成功了