2014-01-10 34 views
0

代码隐藏创建的DataGrid这是一个DataGrid在XAML:与绑定

ItemsSource="{Binding Path=NewContactList,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" ItemsPanel="{StaticResource panelTemplate}" 
ItemTemplate="{StaticResource ListTemplate}" 
DragDrop:DragDropHelper.IsDragSource="true" 
DragDrop:DragDropHelper.IsDropTarget="true" 
DragDrop:DragDropHelper.DragDropTemplate="{StaticResource DragTemplate}" 

我想创建代码隐藏在DataGrid,但我知道如何设置绑定和我的DragDrop:DragDropHelper

有人能帮助我吗?

回答

1

试试这个:

var myDataGrid = new DataGrid(); 
myDataGrid.SetBinding(DataGrid.ItemsSourceProperty, new Binding("NewContactList") { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, Mode = BindingMode.TwoWay }); 
myDataGrid.SetResourceReference(DataGrid.ItemsPanelProperty, "panelTemplate"); 
myDataGrid.SetResourceReference(DataGrid.ItemTemplateProperty, "ListTemplate"); 
myDataGrid.SetValue(DragDropHelper.IsDragSourceProperty, True); 
myDataGrid.SetValue(DragDropHelper.IsDropTargetProperty, True); 
myDataGrid.SetResourceReference(DragDropHelper.DragDropTemplateProperty, "DragTemplate"); 
+0

它适合我的问题,TY –

1

尝试这样:

Dim dataGridObj As New DataGrid() 

Dim itemsSourceBinding As New Binding("NewContactList") 
itemsSourceBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
itemsSourceBinding.Mode = BindingMode.TwoWay 

Dim itemsPanelResource = Me.FindResource("panelTemplate") 
Dim itemTemplateResource = Me.FindResource("ListTemplate") 
Dim dragDropTemplate = Me.FindResource("DragTemplate") 

dataGridObj.SetBinding(ListBox.ItemsSourceProperty, itemsSourceBinding) 
dataGridObj.ItemsPanel = CType(itemsPanelResource, ItemsPanelTemplate) 
dataGridObj.ItemTemplate = CType(itemTemplateResource, DataTemplate) 
dataGridObj.SetValue(DragDropHelper.IsDragSource, True) 
dataGridObj.SetValue(DragDropHelper.IsDropTarget, True) 
dataGridObj.SetValue(DragDropHelper.DragDropTemplate, CType(dragDropTemplate, DataTemplate))