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:
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
void Fetcher_RequestComplete(bool error, string result, int requestsLeft)
{
if (!error)
{
Database.Mods = Parser.ParseMods(result);
}
}
This is all that’s in database.cs
public static class Database
{
public static ObservableCollection<Mod> Mods = new ObservableCollection<Mod>();
}
And this is all that’s in 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.
EDIT: your question is a little bit wrong because you do not BIND something to your grid you simply SET the itemssource
nevertheless there are 2 ways you can achieve what you want.
or
the cleaner way would be MVVM but thats was not your question 🙂