WPF DataGrid:UI不会更新ItemSource

时间:2015-08-01 16:41:28

标签: c# wpf list datagrid itemsource

我使用DataGrid从用户那里获取输入并将其放在List中。我已经使用DataGrid.ItemSource属性将DataGrid绑定到我创建的List。但是,在运行时,当我编辑DataGrid时,绑定到它的List不会自动更新。

首先定义一个自定义类型的List(loadTable)(PedLoad):

//Defines pedestal loads for a footing
struct PedLoad
{
    public string LoadCase { get; set; }    //Load case description
    public double Axial { get; set; }       //Axial force (tension or compression)
    public double Shear { get; set; }       //Shear force
    public double Moment { get; set; }      //Moment
}

//Create the load list
List<PedLoad> loadTable = new List<PedLoad>();

稍后在代码中我将项目(PedLoads)添加到List(loadTable)并将其绑定到DataGrid(dgdLoadTable),如下所示:

//Bind the datagrid to the load table
dgdLoadTable.ItemsSource = loadTable;

//Populate the load case names
DL.LoadCase = "Dead (D)";
LL.LoadCase = "Live (L)";
LLR.LoadCase = "Roof Live (Lr)";
SL.LoadCase = "Snow (S)";
WL.LoadCase = "Wind (W)";
EL.LoadCase = "Seismic (E)";

//Add the loads to the load list/datagrid
loadTable.Add(DL);
loadTable.Add(LL);
loadTable.Add(LLR);
loadTable.Add(SL);
loadTable.Add(WL);
loadTable.Add(EL);

这很好用,除非我更新DataGrid,List(loadTable)跟不上它。每当用户编辑DataGrid时,如何让List更新?

这里是完整的代码,用ObservableCollection修改:

public partial class MainWindow:Window     {         //创建草图对象         Rectangle slab = new Rectangle();         Rectangle pedestal = new Rectangle();

    //Create the load table and the results table
    ObservableCollection<PedLoad> loadTable = new ObservableCollection<PedLoad>();
    ObservableCollection<ServiceResults> resultsTable = new ObservableCollection<ServiceResults>();

    public MainWindow()
    {
        InitializeComponent();

        //Bind the datagrids to the load tables

        dgdResultsTable.ItemsSource = resultsTable;

        //Populate the load case names
        loadTable.Add(new PedLoad { LoadCase = "Dead (D)" });
        loadTable.Add(new PedLoad { LoadCase = "Live (L)" });
        loadTable.Add(new PedLoad { LoadCase = "Roof Live (Lr)" });
        loadTable.Add(new PedLoad { LoadCase = "Snow (S)" });
        loadTable.Add(new PedLoad { LoadCase = "Wind (W)" });
        loadTable.Add(new PedLoad { LoadCase = "Seismic (E)" });

        dgdLoadTable.ItemsSource = loadTable;

        //Analyze the footing
        Analyze();

        //Adjust the color of the sketch objects
        slab.Fill = Brushes.LightSlateGray;
        pedestal.Fill = Brushes.LightSlateGray;

        //Add the sketch objects to the canvas
        cvsSketchArea.Children.Add(slab);
        cvsSketchArea.Children.Add(pedestal);

        //Update the sketch
        UpdateSketch();
    }

    //Analyzes the footing
    private void Analyze()
    {
        //Clear out old results
        resultsTable.Clear();

        //Create a list of service footings
        List<SpreadFooting> serviceFtgs = new List<SpreadFooting>();

        //Populate the list with the basic footing data
        for (int i = 0; i < 13; i++)
        {
            SpreadFooting newFtg = new SpreadFooting();
            newFtg.L = double.Parse(txtFtgLength.Text);
            newFtg.B = double.Parse(txtFtgWidth.Text);
            newFtg.h = double.Parse(txtFtgThickness.Text) / 12;
            newFtg.hp = double.Parse(txtPedHeight.Text);
            newFtg.wp1 = double.Parse(txtPedWidth1.Text) / 12;
            newFtg.wp2 = double.Parse(txtPedWidth2.Text) / 12;
            newFtg.ep = double.Parse(txtPedEcc.Text);
            newFtg.wc = double.Parse(txtConcreteWt.Text) / 1000;
            newFtg.wo = double.Parse(txtOverburden.Text) / 1000;
            serviceFtgs.Add(newFtg);
        }

        //Populate the list with the load data
        //D
        serviceFtgs[0].Description = "D";
        serviceFtgs[0].P = loadTable[0].Axial;
        serviceFtgs[0].V = loadTable[0].Shear;
        serviceFtgs[0].M = loadTable[0].Moment;

        //D+L
        serviceFtgs[1].Description = "D+L";
        serviceFtgs[1].P = loadTable[0].Axial + loadTable[1].Axial;
        serviceFtgs[1].V = loadTable[0].Shear + loadTable[1].Shear;
        serviceFtgs[1].M = loadTable[0].Moment + loadTable[1].Moment;

        //D+Lr
        serviceFtgs[2].Description = "D+Lr";
        serviceFtgs[2].P = loadTable[0].Axial + loadTable[2].Axial;
        serviceFtgs[2].V = loadTable[0].Shear + loadTable[2].Shear;
        serviceFtgs[2].M = loadTable[0].Moment + loadTable[2].Moment;

        //D+S
        serviceFtgs[3].Description = "D+S";
        serviceFtgs[3].P = loadTable[0].Axial + loadTable[3].Axial;
        serviceFtgs[3].V = loadTable[0].Shear + loadTable[3].Shear;
        serviceFtgs[3].M = loadTable[0].Moment + loadTable[3].Moment;

        //D+0.75(L+Lr)
        serviceFtgs[4].Description = "D+0.75(L+Lr)";
        serviceFtgs[4].P = loadTable[0].Axial + 0.75 * (loadTable[1].Axial + loadTable[2].Axial);
        serviceFtgs[4].V = loadTable[0].Shear + 0.75 * (loadTable[1].Shear + loadTable[2].Shear);
        serviceFtgs[4].M = loadTable[0].Moment + 0.75 * (loadTable[1].Moment + loadTable[2].Moment);

        //D+0.75(L+S)
        serviceFtgs[4].Description = "D+0.75(L+S)";
        serviceFtgs[4].P = loadTable[0].Axial + 0.75 * (loadTable[1].Axial + loadTable[3].Axial);
        serviceFtgs[4].V = loadTable[0].Shear + 0.75 * (loadTable[1].Shear + loadTable[3].Shear);
        serviceFtgs[4].M = loadTable[0].Moment + 0.75 * (loadTable[1].Moment + loadTable[3].Moment);

        //Populate the results table
        for (int i = 0; i < 13; i++)
        {
            serviceFtgs[i].Calculate();
            resultsTable.Add(serviceFtgs[i].Results1);
        }

    }

1 个答案:

答案 0 :(得分:1)

使用ObservableCollection代替List

    private void dgdLoadTable_Loaded(object sender, RoutedEventArgs e)
    {
        loadTable = new ObservableCollection<PedLoad>();

        loadTable.Add(new PedLoad { LoadCase = "Dead (D)" });
        loadTable.Add(new PedLoad { LoadCase = "Live (L)" });
        loadTable.Add(new PedLoad { LoadCase = "Roof Live (Lr)" });
        loadTable.Add(new PedLoad { LoadCase = "Snow (S)" });
        loadTable.Add(new PedLoad { LoadCase = "Wind (W)" });
        loadTable.Add(new PedLoad { LoadCase = "Seismic (E)" });

        dgdLoadTable.ItemsSource = loadTable;
    }
    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        loadTable.Add(new PedLoad { LoadCase = "New Item" });
    }

结果:

enter image description here

用户编辑后ObservableCollection肯定会更新: enter image description here