gpt4 book ai didi

C# Windows 通用 : Unable to add items to DataGrid dynamically

转载 作者:太空宇宙 更新时间:2023-11-03 22:34:44 25 4
gpt4 key购买 nike

我有几个TextBox es,一个Button和一个 controls:DataGrid我的项目 MainPage.xaml .当我点击按钮时,我想要 DataGrid更新,但由于某些未知原因它不起作用。

这是 MainPage.xaml 的相关部分

<Button x:Name="calculateButton" Content="Calculate" HorizontalAlignment="Left" VerticalAlignment="Center" Click="calculate_click"/>
<!--Necessary TextBoxes above the Button-->
<controls:DataGrid x:Name="dataGrid">
</controls:DataGrid>

以下是 MainPage.xaml.cs 的相关部分文件:

namespace New_Salary_Calculator
{
public class TableData
{
public Double Basic { get; set; }
public Double Fbp { get; set; }
public Double Inh { get; set; }
public Double Ars { get; set; }
public Double Cur { get; set; }

public TableData(Double basic, Double fbp, Double inh, Double ars, Double cur)
{
this.Basic = basic;
this.Fbp = fbp;
this.Inh = inh;
this.Ars = ars;
this.Cur = cur;
}
}

public sealed partial class MainPage : Page
{
public List<TableData> Data;
public MainPage()
{
this.InitializeComponent();
Data = new List<TableData>();
}

private void calculate_click(object sender, RoutedEventArgs e)
{
int years = Convert.ToInt32(yearInput.Text) - 1;
for (int i = 1; i <= years; ++i)
{
Double pow = Math.Pow(1.09, years);
Double basic = Math.Round(Convert.ToDouble(basicInput.Text) * pow),
fbp = Math.Round(basic * Convert.ToDouble(fbpInput.Text) / 100),
pf = Math.Round(basic * Convert.ToDouble(pfInput.Text) / 100),
grat = Math.Round(basic * Convert.ToDouble(gratInput.Text) / 100);

Double inh = Math.Floor(basic + fbp), ars = Math.Round(fbp + basic + pf + grat);

Double rent = Math.Round(Convert.ToDouble(rentInput.Text) * 12),
food = Math.Round(Convert.ToDouble(foodInput.Text) * pow),
investments = Math.Round(Convert.ToDouble(investmentsInput.Text) * pow),
ins = 100000;

Double cur = inh - rent - food - investments - ins;
Data.Add(new TableData(basic, fbp, inh, ars, cur));
}
dataGrid.ItemsSource = Data;
}
}
}

calculate_click() 的 for 循环的最后一行内,每次循环运行到 List<TableData> 时我都会添加项目对象 Data .在函数的末尾,我分配 dataGrid.ItemsSource作为这个列表。但它所做的只是将 header 添加到 DataGrid而不是其中的实际项目:

点击前: Before

点击后: After

最佳答案

List<>不会自动或动态更新数据。

使用 ObservableCollection<>而不是更新您的 DataGrid即时的。

阅读这些文档以获得进一步的帮助-

Binding to a collection of items

ObservableCollection Class

你的代码应该是这样的 -

    public sealed partial class MainPage : Page
{
private ObservableCollection<TableData> Data;
public MainPage()
{
this.InitializeComponent();
Data = new ObservableCollection<TableData>();
dataGrid.ItemsSource = Data;
}

private void calculate_click(object sender, RoutedEventArgs e)
{
int years = Convert.ToInt32(yearInput.Text) - 1;
for (int i = 1; i <= years; ++i)
{
double pow = Math.Pow(1.09, years);
double basic = Math.Round(Convert.ToDouble(basicInput.Text) * pow),
fbp = Math.Round(basic * Convert.ToDouble(fbpInput.Text) / 100),
pf = Math.Round(basic * Convert.ToDouble(pfInput.Text) / 100),
grat = Math.Round(basic * Convert.ToDouble(gratInput.Text) / 100);

double inh = Math.Floor(basic + fbp), ars = Math.Round(fbp + basic + pf + grat);

double rent = Math.Round(Convert.ToDouble(rentInput.Text) * 12),
food = Math.Round(Convert.ToDouble(foodInput.Text) * pow),
investments = Math.Round(Convert.ToDouble(investmentsInput.Text) * pow),
ins = 100000;

Double cur = inh - rent - food - investments - ins;
Data.Add(new TableData(basic, fbp, inh, ars, cur));
}
}
}

关于C# Windows 通用 : Unable to add items to DataGrid dynamically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55773734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com