Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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 DataGrid绑定属性_C#_Wpf_Datagrid - Fatal编程技术网

C# 未找到WPF DataGrid绑定属性

C# 未找到WPF DataGrid绑定属性,c#,wpf,datagrid,C#,Wpf,Datagrid,我刚刚开始使用WPF,在向DataGrid显示数据时遇到了一个问题 WPF Views\ResultsPage.xaml: 我得到以下错误:System.Windows.Data错误:40:BindingExpression路径错误:“SampleData”应该是DataGrid的DataContext属性或返回IEnumerable的父元素。试试这个: public partial class MainWindow : Window { public MainWindow()

我刚刚开始使用WPF,在向DataGrid显示数据时遇到了一个问题

WPF Views\ResultsPage.xaml:

我得到以下错误:System.Windows.Data错误:40:BindingExpression路径错误:“SampleData”应该是DataGrid的DataContext属性或返回IEnumerable的父元素。试试这个:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    //set the DataContext of the window itself or the DataGrid
        dgResults.DataContext = new ViewModel();
    }
}

public class ViewModel
{
    public ViewModel()
    {
        SampleData = new List<SampleData>();
        SampleData.Add(new SampleData() { WellID = "1" });
        SampleData.Add(new SampleData() { WellID = "2" });
    }

    public List<SampleData> SampleData { get; }
}

您需要创建一个新的SampleData实例,然后将该实例作为列表或ObservableCollection绑定到DataGrid。SampleData应该是ViewModel中的一个属性,如下所示:ObservableCollection SampleData{get;set;}并且不要忘记InotifyPropertyChanged设置窗口的DataContext了吗?否则,您的XAML将假定您的数据来自CodeBehind。我已使用INotifyPropertyChangedThanks更新了代码,以获取您的答案。你的意思是列表sd=新列表??很抱歉,我在SampleData.Add中遇到错误,我使用List List=SampleData.ToList;在那之前。然而,我的DataGrid是空的,我认为它无法解析SampleData,因为它是放置在另一个文件夹中的类。是的,我正在使用您的代码。我刚刚更改了这个SampleData=新列表;列表=SampleData.ToList;addnewsampledata{WellID=1};因为Add方法出错,所以需要设置SampleData属性。在视图中绑定到的就是这个。我的示例代码按原样工作-没有理由在列表中调用ToList…抱歉,我看到您是WPF的专家。正如我所说,我刚刚从Windows窗体转移到WPF。使用您的代码,我得到IEnumerable不包含Add定义。
public class : INotifyPropertyChanged
{
    private string _wellID;

    public string WellID
    {
        get
        {
            return _wellID;
        }
        set
        {
            _wellID = value;
            NotifyPropertyChanged("WellID");
        }
    }
  ...

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    //set the DataContext of the window itself or the DataGrid
        dgResults.DataContext = new ViewModel();
    }
}

public class ViewModel
{
    public ViewModel()
    {
        SampleData = new List<SampleData>();
        SampleData.Add(new SampleData() { WellID = "1" });
        SampleData.Add(new SampleData() { WellID = "2" });
    }

    public List<SampleData> SampleData { get; }
}