我正在使用python.net编写应用程序。我的目标是以编程方式使用数据填充DataGrid。我正在处理的代码如下:
import clr
import System.Threading
System.Threading.Thread.CurrentThread.SetApartmentState(System.Threading.ApartmentState.STA)
clr.AddReference(r"wpf\PresentationFramework")
clr.AddReference(r"wpf\PresentationCore")
clr.AddReference("System.Xml")
from System.IO import StringReader
from System.Xml import XmlReader
from System.Windows.Markup import XamlReader, XamlWriter
from System.Windows import Window, Application, LogicalTreeHelper, MessageBox
from model import Model
from System.Windows.Media import Brushes
from random import random
from System.Windows.Controls import DataGridTextColumn, TextBox
from System.Windows.Data import Binding
xaml = """
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="panel1"
Title="harpia.ml" Height="600" Width="800" Background="#FFFBFBFB">
<Grid Margin="0,0,0,58">
<DataGrid x:Name="dataGrid" IsReadOnly="True" Background="#FFAC5C5C" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="206" Width="449">
<DataGrid.Columns>
</DataGrid.Columns>
</DataGrid>
<Button x:Name="refreshBtn" Content="Button" HorizontalAlignment="Left" Margin="10,221,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="384,221,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.033,-0.086"/>
<Button x:Name="button2" Content="Button" HorizontalAlignment="Left" Height="21" Margin="10,266,0,-36" VerticalAlignment="Top" Width="75"/>
<Button x:Name="button3" Content="Button" HorizontalAlignment="Left" Height="21" Margin="384,266,0,-36" VerticalAlignment="Top" Width="75"/>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="21" Margin="117,266,0,-36" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="236"/>
</Grid>
</Window>
"""
class Student(object):
def __init__(self, name, branch, year):
self.name = name
self.branch = branch
self.year = year
print("A student object is created.")
def print_details(self):
"""
Prints the details of the student.
"""
print("Name:", self.name)
print("Branch:", self.branch)
print("Year:", self.year)
def _button2_Click(s, e):
textBox1 = LogicalTreeHelper.FindLogicalNode(win, 'textBox1')
grid = LogicalTreeHelper.FindLogicalNode(win, 'dataGrid')
col1 = DataGridTextColumn()
col2 = DataGridTextColumn()
col3 = DataGridTextColumn()
grid.Columns.Add(col1)
grid.Columns.Add(col2)
grid.Columns.Add(col3)
col1.Binding = Binding("name")
col2.Binding = Binding("branch")
col3.Binding = Binding("year")
col1.Header = "name"
col2.Header = "branch"
col3.Header = "year"
item = []
item.append(Student("Andre", "Piratas", "1973"))
item.append(Student("Andres", "Piratass", "1973s"))
item.append(Student("Andre3", "Piratas3", "19733"))
item.append(Student("Andre4", "Piratas4", "19734"))
grid.ItemsSource = item
textBox1.Text = str(grid.Items[0])
if __name__ == "__main__":
xr = XmlReader.Create(StringReader(xaml))
win = XamlReader.Load(xr)
_button2 = LogicalTreeHelper.FindLogicalNode(win, 'button2')
_button2.Click += _button2_Click
Application().Run(win)
我能够添加列。但是,代码无法将项(行)添加到DataGrid。有谁知道我该怎么做?
答案 0 :(得分:1)
必须是绑定的东西,尝试在定义绑定后添加列。
col1.Binding = Binding("name")
col2.Binding = Binding("branch")
col3.Binding = Binding("year")
col1.Header = "name"
col2.Header = "branch"
col3.Header = "year"
grid.Columns.Add(col1)
grid.Columns.Add(col2)
grid.Columns.Add(col3)
答案 1 :(得分:1)
我和你有同样的问题。我的解决方法是创建一个DataTable并在DataTable中定义内容,然后传递给DataGrid。这是一个示例:
#Crate a DataTable
data_table = System.Data.DataTable("MyDataTable")
data_table.Columns.Add("name")
data_table.Columns.Add("branch")
data_table.Columns.Add("year")
#Add data
data_table.Rows.Add("Andre", "Piratas", "1973")
data_table.Rows.Add("Andres", "Piratass", "1973s")
#DataTable to DataGrid
data_grid.DataContext = data_table.DefaultView
如果您在这两年中找到了更好的解决方案,我非常愿意知道。
答案 2 :(得分:0)
这是我在Windows下的IronPython中成功完成此操作的方式。 (我没有完全尝试这段代码,所以让我知道我是否忘记了import
或打错字了。)
import wpf
import clr
clr.AddReference("System.Xml")
from System.Windows import Window
class MyItem:
"An item to represent a row in your grid."
def __init__(self, name, branch, year):
self.name = name
self.branch = branch
self.year = year
class MyWindow(Window):
"The window defined in your XAML."
def __init__(self):
# I keep XAML in separate files, instead of a string.
wpf.LoadComponent(self, 'my_window.xaml')
# Here are some items for the grid.
my_items = [MyItem('me', 'b0', 2019), MyItem('you', 'b1', 2020)]
# Since this class is the window defined in the XAML, it is self, and
# the names in the XAML defines its members. Therefore, your 'dataGrid'
# can be accessed like this:
self.dataGrid.ItemsSource = my_items
顺便说一句,因为我写Python,所以我遵循Python约定(PEP8),而是调用dataGrid
data_grid
,即<DataGrid Name="data_grid">
。这只是一个约定,它以ItemsSource
之类的东西来分解,但是我认为这更清楚了XAML中的名称实际上可以作为Python中的对象进行访问。