我似乎无法弄清楚如何将wfp中的数据获取到列表中并绑定到datagrid。
xaml表单显示正常
我已经创建了属性,我创建了一个类来继承基类code360。我在网上尝试了很多资源,但对我不起作用。我试图对输入进行硬编码,以查看它是否会填充网格,但是没有办法。像这样
{firstName = "Tim", lastName = "Joy",
email = "tim@joy.com",
phoneNumber = "0988390243",
amount = 200000 }
这是mainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Windows;
namespace Code360
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public class code360
{
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public string phoneNumber { get; set; }
public decimal amount { get; set; }
}
public class Code360Manager : List<code360>
{
public Code360Manager()
{
Add(new code360() {firstName = "Tim", lastName = "Joy", email = "tim@joy.com", phoneNumber = "0988390243", amount = 200000 });
}
}
private void SubmitButton_Click(object sender, RoutedEventArgs e)
{
//studentGrid.ItemsSource = Code360Manager();
MessageBox.Show("This is to test the firstname", firstName.Text);
}
}
}
这是mainWindow.xaml
<Window x:Class="Code360.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Code360"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="1" Grid.Column="1" FontSize="36"
Text="Code360 Student Record" Grid.ColumnSpan="3"
Margin="0,0,0,10" />
<TextBlock Grid.Column="1" Grid.Row="2"
Text="First Name" Margin="0,0,0,10"/>
<TextBox x:Name="firstName" Grid.Column="2" Grid.Row="2"
Width="150" Margin="0,0,0,10"/>
<TextBlock Grid.Column="3" Grid.Row="2" Text="Last Name" Margin="0,0,0,10"/>
<TextBox x:Name="lastName" Grid.Column="4" Grid.Row="2" Width="150" Margin="0,0,0,10"/>
<TextBlock Grid.Column="1" Grid.Row="3"
Text="Email" Margin="0,0,0,10"/>
<TextBox x:Name="email" Grid.Column="2" Grid.Row="3"
Width="150" Margin="0,0,0,10"/>
<TextBlock Grid.Column="3" Grid.Row="3"
Text="Phone Number" Margin="0,0,0,10"/>
<TextBox x:Name="phonenumber" Grid.Column="4" Grid.Row="3"
Width="150" Margin="0,0,0,10"/>
<TextBlock Grid.Column="1" Grid.Row="4"
Text="Amount" Margin="0,0,0,10"/>
<TextBox x:Name="amount" Grid.Column="2" Grid.Row="4"
Width="150" Margin="0,0,0,10"/>
<Button x:Name="submitButton" Content="Submit"
Grid.Column="4" Grid.Row="4"
Margin="0,0,0,10" Click="SubmitButton_Click" />
<DataGrid x:Name="studentGrid" Grid.Column="1" Grid.Row="5" MinWidth="20" Grid.ColumnSpan="5" Margin="22,0,45,154" Grid.RowSpan="2" ItemsSource="{StaticResource Code360Manager}">
<DataGrid.Columns>
<DataGridTextColumn Header="First Name" Binding="{Binding firstName}" />
<DataGridTextColumn Header="Last Name" Binding="{Binding lastName}" />
<DataGridTextColumn Header="Email"
Binding="{Binding email}" />
<DataGridTextColumn Header="Phone Number" Binding="{Binding phonenumber}" />
<DataGridTextColumn Header="Amount" Binding="{Binding amount}" />
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="{Binding Details}" />
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>
</Grid>
</Grid>
</Window>
答案 0 :(得分:0)
我认为您需要设置DataContext。 在InitializeComponent();之后: DataContext = this; 编辑:(对不起。我发的评论不好)。 您需要将xaml设置为上下文。通常使用MainWindowViewModel。 我更改您的代码以将ItemsSource设置为List <>而不是一个类:
class BaseWidget extends StatefulWidget {
final String title;
BaseWidget(this.title);
_BaseWidgetState createState() => _BaseWidgetState();
}
class _BaseWidgetState extends State<BaseWidget> {
@override
Widget build(BuildContext context) {
return Center(child: Text(widget.title));
}
}
class HomeWidget extends StatefulWidget {
_HomeWidgetState createState() => _HomeWidgetState();
}
class _HomeWidgetState extends State<HomeWidget> {
int _pageIndex = 0;
List<Widget> _children;
@override
void initState() {
super.initState();
_children = [
BaseWidget('Home'),
BaseWidget('Business'),
BaseWidget('School')
];
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Bottom Navigation Bar'),
),
body: IndexedStack(
children: _children,
index: _pageIndex,
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _pageIndex,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home), title: Text('Home')),
BottomNavigationBarItem(
icon: Icon(Icons.business), title: Text('Business')),
BottomNavigationBarItem(
icon: Icon(Icons.school), title: Text('School')),
],
onTap: onTap,
),
),
);
}
void onTap(int index) {
setState(() {
_pageIndex = index;
});
}
}
答案 1 :(得分:0)
1。要使用Binding
,您需要设置DataContext
属性
所以
InitializeComponent();
this.DataContext = this;
2.u需要一个集合来绑定xaml.cs中的DataGrid
List<code360> MyList { get; set; }
//确保您在此列表中有数据
3。ItemsSource
道具将接受收藏,因此您需要将其与收藏绑定
ItemsSource="{Binding MyList }
// note the list name is same as the in 2 ponit
这也不是跟随mvvm
答案 2 :(得分:0)
我将该代码粘贴到了Visual Studio中,除其他外,我遇到了ItemsSource="{StaticResource Code360Manager}"
这一行的问题
在您共享的XAML中,我看不到您在哪里对资源进行了反抗,因此请考虑以下内容。
<Grid.Resources>
<local:Code360Manager x:Key="Code360Manager" />
</Grid.Resources>
在定义网格之后,您将看到datagrid渲染
答案 3 :(得分:0)
网格不会将数据输入作为ItemSource,您需要使用DataContext。像这样:
studentGrid.DataContext = Code360Manager();
如果仍然无法正常运行,请尝试使用ListCollectionView,如下所示:
ListCollectionView alist = new ListCollectionView(Code360Manager());
studentGrid.DataContext = alist;