2016-05-24 10 views
1

コードビハインドを使用して、実行時にDataGridControl(Xceed Community Edition)に列を作成して追加しようとしています。列には、DataTemplateとしてCombobBoxが含まれています。ここで私は今持っているものです。コードビハインドからDataGridControlに列を追加する

  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には、2つの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); 
    } 
} 

は今、私はCellEditorBindingがで発見されていないので、{xcdg:CellEditorBinding}のようComboBox.SelectedValuePropertyへの結合を設定する方法罰金することはできませんアセンブリ。どうやってやるの?

答えて

0

同様のリクエストの前のサポートチケットに次のコードスニペットが見つかりました。

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); 

このコードは、あなたのケースでは動作しませんし、あなたがより多くの助けが必要な場合は、[email protected]

で私達に電子メールを送信することができます
関連する問題