将数据从文本框绑定到数据网格

时间:2013-05-11 11:37:21

标签: c# wpf data-binding

我正在制作一个联系信息程序,用户可以在其中输入姓名,年龄和生日以及其他一些内容到文本框中。单击“确定”后,我希望将数据放入DataGrid

首先我有MainWindow,当您按下创建新联系人按钮时,会打开一个名为CreateContact的新窗口:

CreateContact cc = new CreateContact();
cc.ShowDialog();
然后打开一个新窗口,输入您的姓名和年龄,然后选择生日。然后单击“确定”:

private void cmdOk_Click(object sender, RoutedEventArgs e)
{
     MainWindow m = new MainWindow();
     m.name = txtName.Text;
     m.age = txtAge.Text;
     m.birthDate = dpBirthdate.Text;
     m.ShowContacts()
}

应该在我的ShowContacts中运行我的方法MainWindow

public string name;
public string age;
public string birthDate;

public void ShowContacts()
{
    try
    {
         dgContacts.ItemsSource = LoadContactData();
    }
    catch (Exception e)
    {
         MessageBox.Show("" + e);
    }
}

private List<ContactData> LoadContactData()
{
    List<ContactData> list = new List<ContactData>();
    list.Add(new ContactData()
    {
        Name = name,
        Age = age,
        BirthDate = birthDate
    });            
    return list;
}


public class ContactData
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string BirthDate { get; set; }
}

但没有任何反应,我在DataGrid中没有得到任何数据。我希望有人能告诉我出了什么问题。我真的试图搜索导致DataGrid没有数据的原因。

3 个答案:

答案 0 :(得分:0)

假设dgContacts是数据网格视图控件,您必须调用以下命令以确保绑定数据

  

dgContacts.DataBind();

该程序类似于here解释的程序。

如果您调试了代码,并且此建议无效,请调试并发布绑定到控件的数据。

答案 1 :(得分:0)

简短回答:当你在ShowContacts中更改ItemsSource时,看起来你没有触发PropertyChanged事件。

答案很长:你应该真正关注MVVM。您应该创建一个维护ContactData列表的ViewModel,并绑定到XAML中的该列表(也将您的Contact对话框数据绑定到ContactData对象):

var contact = new ContactData();
var cc = new CreateContact { Datacontext = contact };
cc.ShowDialog();

// ok button has been hit at this point,
// you can check for cancel if you want
(List<ContactData>)(dgContacts.ItemsSource).Add(contact);

// may still need to fire PropertyChanged here depending on binding

答案 2 :(得分:0)

好的,这是我在程序中使用的所有xaml和类。我真的希望有人可以帮忙说出来,出了什么问题以及为什么它不会在数据网格中显示任何数据。

MainWindow.xaml:

 <Window x:Class="KontaktInfo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <DataGrid AutoGenerateColumns="True" Name="dgContacts" HorizontalAlignment="Left" Margin="0,95,0,0" VerticalAlignment="Top" Height="225" Width="517"/>

    <Button x:Name="cmdCreateContact" Content="Opret Kontakt" HorizontalAlignment="Left" Margin="0,68,0,0" VerticalAlignment="Top" Width="80" Click="cmdCreateContact_Click"/>
    <Button x:Name="cmdDeleteContact" Content="Slet Kontakt" HorizontalAlignment="Left" Margin="85,68,0,0" VerticalAlignment="Top" Width="80"/>

</Grid>

MainWindow.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KontaktInfo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string name;
        public string age;
        public string birthDate;

        public MainWindow()
        {
            InitializeComponent();   
        }

        private void cmdCreateContact_Click(object sender, RoutedEventArgs e)
        {
            CreateContact cc = new CreateContact();
            cc.ShowDialog();
        }

        public void ShowContacts()
        {
            try
            {
                dgContacts.ItemsSource = LoadContactData();
            }
            catch (Exception e)
            {
                MessageBox.Show("" + e);
            }
        }

        private List<ContactData> LoadContactData()
        {
            List<ContactData> list = new List<ContactData>();
            list.Add(new ContactData()
            {
                Name = name,
                Age = age,
                BirthDate = birthDate
            });

            return list;
        }

    }

public class ContactData
    {
        public string Name { get; set; }
        public string Age { get; set; }
        public string BirthDate { get; set; }
    }
}

CreateContact.xaml:

     <Window x:Class="KontaktInfo.CreateContact"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CreateContact" Height="403" Width="300">
    <Grid>
        <Label Content="Navn" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtName" HorizontalAlignment="Left" Height="23" Margin="10,36,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="272"/>
        <Label Content="Alder" HorizontalAlignment="Left" Margin="10,64,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtAge" HorizontalAlignment="Left" Height="23" Margin="10,90,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="272"/>
        <Label Content="Fødselsdag" HorizontalAlignment="Left" Margin="10,118,0,0" VerticalAlignment="Top"/>
        <Button Name="cmdOk" Content="OK" HorizontalAlignment="Left" Margin="127,341,0,0" VerticalAlignment="Top" Width="75" Click="cmdOk_Click"/>
        <Button Name="cmdCancel" Content="Annuller" HorizontalAlignment="Left" Margin="207,341,0,0" VerticalAlignment="Top" Width="75"/>
        <DatePicker Name="dpBirthdate" HorizontalAlignment="Left" Margin="10,144,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>

CreateContact.cs:

     using System;
     using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace KontaktInfo
{
    /// <summary>
    /// Interaction logic for CreateContact.xaml
    /// </summary>
    public partial class CreateContact : Window
    {
        public CreateContact()
        {
            InitializeComponent();
        }

        private void cmdOk_Click(object sender, RoutedEventArgs e)
        {
            MainWindow m = new MainWindow();
            m.name = txtName.Text;
            m.age = txtAge.Text;
            m.birthDate = dpBirthdate.Text;
            m.ShowContacts();



        }
    }
}