Cannot populate a WPF DataGrid using Python

我只是一个虾纸丫 提交于 2021-02-19 12:50:53

问题


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

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

Doug

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

回答1:


I don't see you creating data_Table instance.

DataTable data_table = CreateDataTable();

Plus try this !

myDataGrid.ItemsSource = data_table.DefaultView;

and in XAML

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



回答2:


OK,

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

"import System.Windows.Data as WPFBindings"

to

"import System.Data as WPFData"

Then took the advice of Hamas and changed,

"myDataGrid.DataContext = data_table.DefaultView" 

to

myDataGrid.ItemsSource= data_table.DefaultView 

Thanks Hammas!!




回答3:


For completeness, here is the working code. Note that by using the DataTable I was able to remove all of the manual column, header and Binding setup

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


来源:https://stackoverflow.com/questions/53983209/cannot-populate-a-wpf-datagrid-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!