早上好。
我正在尝试从Create a custom DataGrid's ItemsSource复制解决方案,但我的行没有填充。
列显示正确的标题,但网格中没有数据。
我是WPF和XAML的新手,有谁能告诉我我做错了什么?
using System;
using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
namespace WPFBindingDataGridTest2
{
public partial class MainWindow : Window
{
public ObservableCollection<MyObject> myList { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
testClass = new ObservableCollection<TestClass>();
testClass.Add(new TestClass(NameValue: "John"));
testClass.Add(new TestClass(NameValue: "Frank"));
testClass.Add(new TestClass(NameValue: "Sarah"));
testClass.Add(new TestClass(NameValue: "David"));
}
}
class TestClass : INotifyPropertyChanged
{
private string name;
public TestClass(string NameValue)
{
this.Name = NameValue;
}
public String Name
{
get { return name; }
set { this.name = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
和XAML ......
<Grid>
<DataGrid x:Name="dataGrid" ItemsSource="{Binding testClass}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
我得到的错误是
System.Windows.Data Error: 40 :
BindingExpression path error:
'testClass' property not found on 'object'''MainWindow'
(Name='')'.BindingExpression:
Path=testClass;
DataItem='MainWindow' (Name='');
target element is 'DataGrid' (Name='dataGrid');
target property is 'ItemsSource' (type 'IEnumerable')
我觉得我如此接近这一点,但我只是缺少一件事 可能数据上下文错了吗?
答案 0 :(得分:1)
分配DataContext时,myList
为null
。在以后创建时,属性不会报告有关更改(通过事件)
快速解决方法是更改操作顺序:
public MainWindow()
{
myList = new ObservableCollection<MyObject>
{
new MyObject() { MyID = "6222" },
new MyObject() { MyID = "12" },
new MyObject() { MyID = "666" }
};
this.DataContext = this;
InitializeComponent();
}
还修复了列绑定(Binding="{Binding MyID}"
)或属性名称(MyId
),因为绑定路径应该与属性名称匹配
但我建议使用INotifyPropertyChanged
属性创建一个单独的类(实现myList
),并使用该类的实例(视图模型)来设置窗口的DataContext
。
答案 1 :(得分:0)
想出来。
var arrayOne = [{"Name":"job","SubscriptionGUID":"8ead7edfa460"},{"Name":"TestJobSQL","SubscriptionGUID":"09e7dbff7779"}];
var arrayTwo = [{"UserSubscriptionID":13,"SubscriptionGUID":"8ead7edfa460","Name":"job"}];
var arrayDiff = [];
arrayOne.forEach(function(item, index){
var found = false;
arrayTwo.forEach(function(item1, index1){
if(item.Name == item1.Name) {
found = true;
}
})
if(found == false) {
arrayDiff.push({ Name : item.Name});
}
})
console.log(arrayDiff);
XAML
using System;
using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
namespace WPFBindingDataGridTest2
{
public partial class MainWindow : Window
{
private ObservableCollection<TestClass> testClass;
public MainWindow()
{
InitializeComponent();
testClass = new ObservableCollection<TestClass>();
testClass.Add(new TestClass(NameValue: "John"));
testClass.Add(new TestClass(NameValue: "Frank"));
testClass.Add(new TestClass(NameValue: "Sarah"));
testClass.Add(new TestClass(NameValue: "David"));
// this.DataContext = this does not appear to be nessecary if it the class is stored in the main window
dataGrid.ItemsSource = testClass;
}
}
class TestClass : INotifyPropertyChanged
{
private string name;
public TestClass(string NameValue)
{
this.Name = NameValue;
}
public String Name
{
get { return name; }
set { this.name = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}