数据网格行为空白

本文关键字:空白 网格 数据网 数据 | 更新日期: 2023-09-27 18:17:27

我的数据网格给了我正确的行数,但行中没有数据。

这是我的WPF代码
<Grid AllowDrop="True">
    <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="c1" Header="Full Name" Binding="{Binding FullNames}" Width="200"/>
            <DataGridTextColumn x:Name="c2" Header="Age" Binding="{Binding Ages}" Width="200"/>
        </DataGrid.Columns>
    </DataGrid>
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="128,296,0,3" Width="75" Click="button_Click"/>
</Grid>

下面是我的

代码
    public partial class MainWindow : Window
{
    public string FullNames { get; set; }
    public int Ages { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        this.dataGrid.DataContext = GetInfo();
    }

    private List<string> GetInfo()
    {
        List<string> list = new List<string>();
        List<int> listAge = new List<int>();
        list.Add(FullNames = "User 1" );
        list.Add(FullNames = "User 2");
        list.Add(FullNames = "User 3");
        list.Add(FullNames = "User 4");
        list.Add(FullNames = "User 5");
        listAge.Add(Ages = 35);
        listAge.Add(Ages = 34);
        listAge.Add(Ages = 10);
        listAge.Add(Ages = 8);
        listAge.Add(Ages = 4);
        return list;
    }
}

提前感谢。顺便说一下,我必须写这个,因为stackoverflow说我需要更多的细节。我以为我写的那一点点代码就足够了,但我猜不是,lol

数据网格行为空白

当这些属性被分配时,UI不会更新,以更新UI:1)使用ObservableCollection,这是一个列表,当项目被添加或删除时更新UI。2)定义一个类,如:

public class Person: DependencyObject
{
    public string name
    {
        get { return (string)GetValue(nameProperty); }
        set { SetValue(nameProperty, value); }
    }
    // Using a DependencyProperty as the backing store for name.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty nameProperty =
        DependencyProperty.Register("name", typeof(string), typeof(Person), new PropertyMetadata("Fred"));

    public int age
    {
        get { return (int)GetValue(ageProperty); }
        set { SetValue(ageProperty, value);
            if (fireshower != null)
                fireshower.Value = value;
        }
    }
    // Using a DependencyProperty as the backing store for age.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ageProperty =
        DependencyProperty.Register("age", typeof(int), typeof(RuleTileInfo), new PropertyMetadata(0));

}

这为你做了两件事:它将使你的代码更容易扩展,现在信息被整齐地组织起来,如果一个人的年龄改变了,UI将收到一个更新。3)虽然可以将数据上下文设置为Observable集合并使用ItemsSource={Binding},但更好的做法是将UI中显示的所有对象置于视图模型中,并将窗口的数据上下文设置为视图模型。然后绑定到该视图模型中列表的名称。这种情况下的虚拟机可能是:

public class WindowVM: DependencyObject
{
public ObservableCollection<Person> People
{
    get { return (string)GetValue(PeepleProperty); }
    set { SetValue(PeopleProperty, value); }
}
// Using a DependencyProperty as the backing store for name.  This enables animation, styling, binding, etc...
//ect
}

这将有助于组织您的程序,使其更容易理解和更易于维护。提示:依赖属性手工输入很麻烦,使用代码片段(输入propdp并按tab键两次,填写字段,按tab键在它们之间切换,VS会为你填写多余的信息)