无法使用Python填充WPF DataGrid

时间:2018-12-31 03:07:20

标签: python .net wpf data-binding python.net

使用Python.NET将数据绑定到WPF DataGrid时遇到问题

下面显示了代码,我尝试了三种不同的绑定数据的方法-每种方法都失败,并且错误消息作为注释包含在下面的代码中

如果我不尝试添加数据,则datagarid正确显示带有标题。但是我无法用任何数据填充网格。

任何帮助都会感激不尽!!

道格

import clr

#.NET references
import System
import System.Windows.Controls as WPFControls
import System.Windows.Data as WPFBindings


def getCustomToolPropertyContent():

   #Create a Grid
   my_Grid = WPFControls.Grid()

   #Add 1 Row and One Column
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.ColumnDefinitions.Add(WPFControls.ColumnDefinition())

   # Create a DataGrid
   myDataGrid = WPFControls.DataGrid()

   #Create three columns
   column_1 = WPFControls.DataGridTextColumn()
   column_1.Header = "ID"
   column_1.Binding = WPFBindings.Binding("id")

   column_2 = WPFControls.DataGridTextColumn()
   column_2.Header = "Title"
   column_2.Binding = WPFBindings.Binding("title")

   column_3 = WPFControls.DataGridTextColumn()
   column_3.Header = "Content"
   column_3.Binding = WPFBindings.Binding("content")

   #Add the three columns to the datagrid
   myDataGrid.Columns.Add(column_1)
   myDataGrid.Columns.Add(column_2)
   myDataGrid.Columns.Add(column_3)

# Data table approach....  
# Fails with
#  AttributeError : DataTable

   #Create a DataTable
   data_table = WPFBindings.DataTable("MyDataTable")
   data_table.Columns.Add("id")
   data_table.Columns.Add("title")
   data_table.Columns.Add("content")

   #Add data 
   data_table.Rows.Add("Andre", "Piratas", "1973")
   data_table.Rows.Add("Andres", "Piratass", "1973s")

   #DataTable to DataGrid
   myDataGrid.DataContext = data_table.DefaultView   


# Item Source Approach   
# Fails with 
#  TypeError: 'list' value cannot be converted to System.Collections.IEnumerable  
#   items = []

#   items.append(Student(id="1", title="Piratas", content="1973"))
#   items.append(Student(id="2", title="XXXX", content="1974"))  

#   myDataGrid.ItemsSource = items


# Items.Add approach
# Fails with 
#  TypeError: No method matches given arguments

#   myDataGrid.Items.Add(Student(id="1", title="Piratas", content="1973"))  



   # Position the DataGrid in the first row, column of the Grid
   WPFControls.Grid.SetRow(myDataGrid, 0)
   WPFControls.Grid.SetColumn(myDataGrid, 0)

   #Add the DataGrid to the Grid 
   my_Grid.Children.Add(myDataGrid)


   # Return the Grid   
   return my_Grid

3 个答案:

答案 0 :(得分:2)

我看不到您正在创建data_Table实例。

DataTable data_table = CreateDataTable();

加尝试一下!

myDataGrid.ItemsSource = data_table.DefaultView;

和XAML

<DataGrid Name="myDataGrid" ItemsSource="{Binding}">

答案 1 :(得分:2)

确定

我明白了-我对DataTable的导入错误。 已更改

"import System.Windows.Data as WPFBindings"

"import System.Data as WPFData"

然后接受哈马斯的建议并进行了更改,

"myDataGrid.DataContext = data_table.DefaultView" 

myDataGrid.ItemsSource= data_table.DefaultView 

谢谢哈马斯!

答案 2 :(得分:1)

为完整起见,这是工作代码。 请注意,通过使用DataTable,我可以删除所有手动列,标题和绑定设置

import clr

#.NET references
import System.Windows.Controls as WPFControls
import System.Data as WPFData

def getCustomToolPropertyContent():

   #Create a Grid
   my_Grid = WPFControls.Grid()

   #Add 1 Row and One Column
   my_Grid.RowDefinitions.Add(WPFControls.RowDefinition())
   my_Grid.ColumnDefinitions.Add(WPFControls.ColumnDefinition())

   # Create a DataGrid
   myDataGrid = WPFControls.DataGrid()

# Data table approach....  
   #Create a DataTable
   data_table = WPFData.DataTable("MyDataTable")
   data_table.Columns.Add("ID")
   data_table.Columns.Add("Title")
   data_table.Columns.Add("Content")

   #Add data 
   data_table.Rows.Add("Andre", "Piratas", "1973")
   data_table.Rows.Add("Andres", "Piratass", "1973s")

   myDataGrid.ItemsSource = data_table.DefaultView     

   # Position the DataGrid in the first row, column of the Grid
   WPFControls.Grid.SetRow(myDataGrid, 0)
   WPFControls.Grid.SetColumn(myDataGrid, 0)

   #Add the DataGrid to the Grid 
   my_Grid.Children.Add(myDataGrid)

   # Return the Grid   
   return my_Grid