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

标签 c# uwp datagrid windows-10-universal

我有几个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/

相关文章:

c# - 使桌面客户端无需更新即可使用较新的 EF 数据库迁移

c# - AttachedToParent 任务混淆

c# - 如何删除后台页面以退出应用程序

c# - 如何在Windows 10 Universal App中制作磨砂玻璃效果?

c# - WPF 数据网格 : How To Keep Selected Rows Highlighted?

c# - 从C#中的异常传递错误代码

mysql - 使用 Visual Studio 在 WINDOWS STORE 中发布带有 Mysql.dll 的 UWP 时出错

c# - 使用AudioRender设备作为AudioGraph UWP的输入

wpf - 数据网格列不更新

WPF Datagrid 行号