0

This is in a DataGrid in 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}"

I want to create the DataGrid in Codebehind, but i know how to set the Bindings and my DragDrop:DragDropHelper

Could somebody help me?

2 Answers 2

1

Try this:

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");
Sign up to request clarification or add additional context in comments.

Comments

1

Try something like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.