繁体   English   中英

WPF DataGrid:UI不会更新ItemSource

[英]WPF DataGrid: UI Does Not Update ItemSource

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

我首先定义一个自定义类型(PedLoad)的列表(loadTable):

//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)添加到列表(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时,列表(loadTable)跟不上它。 每当用户编辑DataGrid时如何更新列表?

这是完整的代码,使用ObservableCollection进行了修改:

公共局部类MainWindow:Window {//创建草图对象Rectangle slab = new Rectangle(); 矩形基座= 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);
        }

    }

使用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" });
    }

结果:

在此处输入图片说明

用户编辑后, ObservableCollection肯定会更新: 在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM