【问题标题】:Create DataGrid in codebehind with bindings使用绑定在代码隐藏中创建 DataGrid
【发布时间】:2014-01-10 18:27:17
【问题描述】:

这是在 XAML 中的 DataGrid 中:

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}"

我想在 Codebehind 中创建 DataGrid,但我知道如何设置绑定和我的 DragDrop:DragDropHelper

有人可以帮我吗?

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案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");
    

    【讨论】:

      【解决方案2】:

      试试这样的:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-14
        相关资源
        最近更新 更多