0
這是我Mainwindow.cs
代碼如何行添加到WPF網格MVVM
Property p = new Property(RandomColumnName(),RandomColumnValues());
records.Add(new Record(p));
ColumnCollection = new ObservableCollection<DataGridColumn>();
foreach (var column in columns) // creates grid with the specified columns
{
var binding = new Binding(string.Format("Properties[{0}].Value", column.Index));
ColumnCollection.Add(new DataGridTextColumn() { Header = column.Name, Binding = binding });
}
,並在我的XAML
<StackPanel Margin="10,4,0,0">
<DataGrid
x:Name="dataGrid"
cust:DataGridColumnsBehavior.BindableColumns="{Binding ColumnCollection}"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=records}"/>
</StackPanel>
public class Property:INotifyPropertyChanged
{
public string Name { get; set; }
public object Value { get; set; }
public Property(string name,object value)
{
this.Name = name;
this.Value = value;
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Record
{
private readonly ObservableCollection<Property> _properties = new ObservableCollection<Property>();
public Record(params Property[] properties)
{
foreach (var property in properties)
{
_properties.Add(property);
}
}
public ObservableCollection<Property> Properties
{
get { return _properties; }
}
}
現在添加的只能有一個行的列。
如何添加多行?
感謝
「記錄」是什麼類型? – 2015-02-09 10:12:12
@Sinatr - 不,在這種情況下,它可能是INotifyCollectionChanged的實現,具體取決於「records」的類型 – 2015-02-09 11:20:21