【问题标题】:Why DataGrid adds empy elements on DataGrid.Items.Add?为什么 DataGrid 在 DataGrid.Items.Add 中添加空元素?
【发布时间】:2019-06-05 15:16:09
【问题描述】:

在我需要的 WPF 中,然后我单击按钮,我正在向 DataGrid 添加带有信息的新行。但是比我点击按钮,创建新行,但它的填充是空的,而不是任何字符串。如何解决?感谢您的提前。

XAML 代码:

<DataGrid Name="ClicksGrid" HorizontalAlignment="Left" Height="204" Margin="348,20,0,0" AutoGenerateColumns="False" CanUserAddRows="True" VerticalAlignment="Top" Width="354">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" IsReadOnly="True" Binding="{Binding Path=ID}" Width="40" />
                <DataGridTextColumn Header="ClickType" IsReadOnly="True" Binding="{Binding Path=Type}" Width="120" />
                <DataGridTextColumn Header="Time" IsReadOnly="True" Binding="{Binding Path=Time}" Width="193"/>
            </DataGrid.Columns>
        </DataGrid>

c#代码:

// click class 
public class Click
    {
        public string ID;
        public string Type;
        public string Time;

        public Click(string ID, string Type, string Time)
        {
            this.ID = ID;
            this.Type = Type;
            this.Time = Time;
        }
    }


// by button clicking calling this method:

 private void TableUpdate()
        {
            IDnum++;

            //test strings:
            var cl = new Click("asd", "sdad", "asds");


            ClicksGrid.Items.Add(cl);   
        }

【问题讨论】:

    标签: c# wpf wpfdatagrid


    【解决方案1】:

    这样改变Click类:

        public class Click
        {
            public string ID { get; set; }
            public string Type { get; set; }
            public string Time { get; set; }
    
            public Click(string ID, string Type, string Time)
            {
                this.ID = ID;
                this.Type = Type;
                this.Time = Time;
            }
        }
    

    【讨论】:

    • 你需要类属性中的访问器
    猜你喜欢
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多