无法使用Python填充WPF DataGrid [英] Cannot populate a WPF DataGrid using Python

查看:93
本文介绍了无法使用Python填充WPF DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem binding data to a WPF DataGrid using Python.NET

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

The code is shown below, and I've tried three different approaches to binding data - each fails and the error message is included as a comment in the code below

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

If I do not attempt to add data, the datagarid displays correctly with headers. But I'm unable to populate the grid with any data.

将不胜感激地收到任何帮助!!!

Any help will be gratefully received!!!

道格

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

推荐答案

好,

我明白了-我为DataTable导入了错误的内容.已更改

I got it - I had the wrong import for the DataTable. Changed

"import System.Windows.Data as WPFBindings"

"import System.Data as WPFData"

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

Then took the advice of Hamas and changed,

"myDataGrid.DataContext = data_table.DefaultView" 

myDataGrid.ItemsSource= data_table.DefaultView 

谢谢哈马斯!!

这篇关于无法使用Python填充WPF DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆