简体   繁体   English

如何以编程方式将WPF Datagrid绑定到自定义对象的ObservableCollection?

[英]How do I programmatically bind a WPF Datagrid to a ObservableCollection of custom objects?

I've been searching for ages and haven't found any solution. 我一直在寻找年龄,还没有找到任何解决方案。 Title basically covers what I'm trying to accomplish. 标题基本上涵盖了我要完成的工作。 Here's what I have so far that clearly isn't working with irrelevant stuff cut out. 到目前为止,这是我显然无法处理不相关内容的内容。

In MainWindow.xaml.cs: 在MainWindow.xaml.cs中:

public MainWindow()
{
    InitializeComponent();

    ModGrid = new DataGrid();
    ModGrid.CanUserReorderColumns = false;

    DataGridTextColumn Name = new DataGridTextColumn();
    Name.Header = "Name";
    Name.MinWidth = 45;
    Name.Width = (DataGridLength)(new DataGridLengthConverter()).ConvertFromString("*");
    Binding nameBinding = new Binding("Name");
    nameBinding.Mode = BindingMode.OneWay;
    Name.Binding = nameBinding;
    ModGrid.Columns.Add(Name);

    DataGridTextColumn Tags = new DataGridTextColumn();
    Tags.Header = "Tags";
    Tags.MinWidth = 40;
    Tags.Width = 200;
    Binding tagsBinding = new Binding("Tags");
    tagsBinding.Mode = BindingMode.OneWay;
    Tags.Binding = tagsBinding;
    ModGrid.Columns.Add(Tags);

    // There are more columns but you get the idea

    ModGrid.ItemsSource = Database.Mods;
}

private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
{
    ModGrid.DataContext = Database.Mods;
}

Later I add items to Database.Mods 稍后我将项目添加到Database.Mods

void Fetcher_RequestComplete(bool error, string result, int requestsLeft)
{
    if (!error)
    {
        Database.Mods = Parser.ParseMods(result);
    }
}

This is all that's in database.cs 这就是database.cs中的全部内容

public static class Database
{
    public static ObservableCollection<Mod> Mods = new ObservableCollection<Mod>();
}

And this is all that's in mod.cs 这就是mod.cs中的全部内容

public class Mod
{
    public Mod() { }

    public Image Status;
    public string Name;
    public string Description;
    public string Author;
    public string Tags;
    public int Views;
    public string Link;
    public string[] Versions;
    public string[] TagsList;
}

Sorry about the code spam but I haven't seen how to do this entirely programmatically and it's necessary that I do. 对不起,代码垃圾邮件很抱歉,但是我还没有看到如何完全以编程方式进行此操作,因此有必要这样做。 Any responses at all are very appreciated! 任何答复都非常感谢!

Edit: 编辑:

While I did find an answer, if your in this situation I've found it would be much better to just make a custom control and have access from the XAML that way. 虽然我确实找到了答案,但是如果您在这种情况下,我发现最好做一个自定义控件并以这种方式从XAML进行访问会更好。

EDIT: your question is a little bit wrong because you do not BIND something to your grid you simply SET the itemssource 编辑:您的问题有点错误,因为您没有将某些内容绑定到网格中,只需设置 itemssource

nevertheless there are 2 ways you can achieve what you want. 但是,有两种方法可以实现您想要的。

void Fetcher_RequestComplete(bool error, string result, int requestsLeft)
{
 if (!error)
 {
    Database.Mods.AddRange(Parser.ParseMods(result));//you can use foreach or write AddRange extension
 }
}

or 要么

void Fetcher_RequestComplete(bool error, string result, int requestsLeft)
{
 if (!error)
 {
    ModGrid.ItemsSource = Parser.ParseMods(result));
 }
}

the cleaner way would be MVVM but thats was not your question :) 较干净的方法是MVVM,但这不是您的问题:)

Your Mod class needs to implement INotifyPropertyChanged to update the screen appropriately. 您的Mod类需要实现INotifyPropertyChanged来适当地更新屏幕。

How To: Implement Property Notification 如何:实施财产通知

尝试

 protected void NotifyPropertyChanged(String info)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM