回复 1楼来爱清的帖子
1.请问你说的是C1DataGrid的动态绑定么?
我这里没有Demo,但帮你找了基本的解决思路和相关代码。
第一步:假设一个数据源,里面有FirstName和LastName两列:
- public class Property : INotifyPropertyChanged
- {
- public Property(string name, object value)
- {
- Name = name;
- Value = value;
- }
- public string Name { get; private set; }
- public object Value { get; set; }
- 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; }
- }
- }
复制代码
第二步:XMAL的代码绑定数据源应该这样写:
- <c1:C1DataGrid AutoGenerateColumns="false" ItemsSource="{Binding Path=Records}" />
复制代码
第三步:初始化数据,然后动态绑定列:
- var records = new ObservableCollection<Record>();
- records.Add(new Record(new Property("FirstName", "Paul"), new Property("LastName", "Stovell")));
- records.Add(new Record(new Property("FirstName", "Tony"), new Property("LastName", "Black")));
- var columns = records.First()
- .Properties
- .Select((x, i) => new { Name = x.Name, Index = i })
- .ToArray();
- foreach (var column in columns)
- {
- var binding = new Binding(string.Format("Properties[{0}].Value", column.Index));
- C1.WPF.DataGrid.DataGridTextColumn textColumn = new C1.WPF.DataGrid.DataGridTextColumn();
- textColumn.Header = column.Name;
- textColumn.Binding = binding;
- this.c1DataGrid1.Columns.Add(textColumn);
- }
复制代码
2.可以在XAML代码中通过IsReadOnly属性设置为只读。
- <c1:DataGridTextColumn Header="规则名称" Binding="{Binding RULE_NAME, Mode=TwoWay}" IsReadOnly="True" />
- <c1:DataGridNumericColumn Header="问题单据数" Binding="{Binding EXP_ORD_COUNT, Mode=TwoWay}" SortMemberPath="EXP_ORD_COUNT" IsReadOnly="False"/>
复制代码 |