我是 WPF 的初学者,我尝试在 WPF 中绑定到 DataGrid。
XAML 代码:
<Grid x:Name="LayoutRoot">
<Grid HorizontalAlignment="Left" Height="440" VerticalAlignment="Top"
Width="632">
<DataGrid HorizontalAlignment="Left" Height="420" Margin="10,10,0,0"
VerticalAlignment="Top" Width="603" ItemsSource="{Binding
Source=MailCollection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="id" Binding="{Binding Id}"/>
<DataGridTextColumn Header="nazwa" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid>
这里是MailTpl
类:
public class MailTpl
{
public string Id { get; set; }
public string Name { get; set; }
}
这是我如何进行绑定:
public partial class WindowDataGridTest : Window
{
ObservableCollection<MailTpl> _MailCollection =
new ObservableCollection<MailTpl>();
public ObservableCollection<MailTpl> MailCollection
{ get { return _MailCollection; } }
public WindowDataGridTest()
{
_MailCollection.Add(new MailTpl
{ Id= "abbb", Name = "badfasdf" });
_MailCollection.Add(new MailTpl
{ Id = "asasdfasdfdf", Name = "basdfasdfaa" });
this.InitializeComponent();
// Insert code required on object creation below this point.
}
}
我不知道为什么它不起作用。有什么线索吗?网格不显示任何值。