[英]Adding a column to DataGridControl from code-behind
我試圖通過代碼隱藏在運行時創建並添加一個列到DataGridControl(Xceed Community Edition)。 該列包括CombobBox作為DataTemplate。 這就是我現在擁有的:
CellEditor editor = new CellEditor();
DataTemplate editTemplate = editor.EditTemplate = new DataTemplate();
FrameworkElementFactory templateCbox = new FrameworkElementFactory(typeof(ComboBox)) { Name = "cmbMain" };
templateCbox.SetBinding(ComboBox.SelectedValuePathProperty, new Binding("CountryID"));
templateCbox.SetBinding(ComboBox.DisplayMemberPathProperty, new Binding("Name"));
templateCbox.SetBinding(ComboBox.ItemsSourceProperty, new Binding("DataContext.Country") { RelativeSource = new RelativeSource() { AncestorType = typeof(Window) } });
templateCbox.SetBinding(ComboBox.SelectedValueProperty, new CellEditorContext());
editTemplate.VisualTree = templateCbox;
Column clmAdditional = new Column() { FieldName = "CountryID", CellEditorDisplayConditions = CellEditorDisplayConditions.Always };
clmAdditional.CellEditor = editor;
_dgvMain.Columns.Add(clmAdditional);
ViewModel包含兩個DataTable:
public class ViewMode
{
public DataTable Address { get; set; }
public DataTable Country { get; set; }
public ViewMode()
{
Address = new DataTable();
Address.Columns.Add("HouseNumberAdd", typeof(string));
Address.Columns.Add("City", typeof(string));
Address.Columns.Add("Date", typeof(DateTime));
Address.Columns.Add("Used", typeof(bool));
Address.Columns.Add("CountryID", typeof(int));
Address.Rows.Add("Random Address 1", "Krakov", DateTime.Now, true, 1);
Address.Rows.Add("Random Address 2", "Kharkiv", DateTime.Now, true, 2);
Address.Rows.Add("Random Address 3", "Moscow", DateTime.Now, false, 3);
Address.Rows.Add("Random Address 4", "Santiago", DateTime.Now, true, 1);
Country = new DataTable("Country");
Country.Columns.Add("Name", typeof(string));
Country.Columns.Add("CountryID", typeof(int));
Country.Rows.Add("America", 1);
Country.Rows.Add("Zimbabve", 2);
Country.Rows.Add("Cayman", 3);
}
}
現在,我{xcdg:CellEditorBinding}
如何設置與{xcdg:CellEditorBinding}
ComboBox.SelectedValueProperty
綁定,因為在程序集中找不到CellEditorBinding
。 我怎樣才能做到這一點?
我在以前的支持服務單中找到了類似請求的以下代碼段。
Binding bin = new Binding();
bin.Mode = BindingMode.TwoWay;
bin.Path = new PropertyPath("(0).(1)", Cell.ParentCellProperty, Cell.ContentProperty);
bin.RelativeSource = RelativeSource.Self;
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(ComboBox));
factory.SetValue(ComboBox.SelectedValueProperty, bin);
如果此代碼在您的情況下不起作用並且您需要更多幫助,可以發送電子郵件至support@xceed.com
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.