我正在尝试将数据网格(WPF C#4.0与WPF Toolkit)绑定到结构的字段。我基本上有一个包含两列的数据网格。第一个我想成为标签,第二个我想成为一个结构的成员。我希望将这一系列的行视为一个单独的结构,然后可以在我的程序中的其他地方使用。
目前我有一个程序为每一行添加一些类的新实例,但我更愿意将它全部视为一个......有没有办法?
由于
答案 0 :(得分:1)
/// <summary>
/// DataGridRowViewModel
/// </summary>
public class DataGridRowViewModel:BaseViewModel
{
/// <summary>
/// structureField
/// </summary>
private string structureField;
private string lableText;
/// <summary>
/// StructureField
/// </summary>
public string StructureField
{
get { return structureField; }
set
{
structureField= value;
OnPropertyChanged("StructureField");
}
}
/// <summary>
/// StructureField
/// </summary>
public string LableText
{
get { return lableText; }
set
{
lableText= value;
OnPropertyChanged("LableText");
}
}
}
/// <summary>
/// DataGridRowViewModel
/// </summary>
public class MainViewModel:BaseViewModel
{
/// <summary>
/// structureField
/// </summary>
private ObservableCollection<DataGridRowViewModel> rowCollection;
//Make Property with INotifyPropertyChanged
/// <summary>
/// Default Constructor
/// </summary>
public MainViewModel()
{
RowCollection = new ObservableCollection<DataGridRowViewModel>();
FillCollectionWithStructureFields();
}
private void FillCollectionWithStructureFields()
{
//Fill Add New Instances of DataGridRowViewModel with required Label
// and Structure Filead Values
}
}
使用必需的列将集合绑定到DataGrid ...