[英]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" });
}
結果:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.