Replies: 2 comments
-
Dear Jake, I understand you're facing difficulties binding a DataGrid to a dynamic class in Avalonia. I suggest you refer to the open-source project at https://github.com/dotnet9/CodeWF.Toolbox. In this project, they use a dictionary to handle dynamic columns. For instance, they define a class like this: public class LanguageProperty
{
public string? Key { get; set; }
public Dictionary<string, string>? Values { get; set; }
} Here, the The binding code is as follows: _languagePropertyDataGrid.Columns.Add(new DataGridTextColumn()
{
Header = nameof(LanguageProperty.Key),
Binding = new CompiledBindingExtension(new CompiledBindingPathBuilder()
.Property(new ClrPropertyInfo(nameof(LanguageProperty.Key),
obj => ((LanguageProperty)obj).Key,
(_, _) => { },
typeof(string)),
PropertyInfoAccessorFactory.CreateInpcPropertyAccessor)
.Build())
});
var cultureNames = SelectedClassItem.Properties.First().Values!.Keys.ToList();
var propertyColumns = cultureNames.Select(cultureName => new DataGridTextColumn()
{
Header = cultureName,
Binding = new CompiledBindingExtension(new CompiledBindingPathBuilder()
.Property(new ClrPropertyInfo(cultureName,
obj =>
{
((LanguageProperty)obj).Values!.TryGetValue(cultureName, out var value);
return value;
},
(obj, value) =>
{
if (value is string newValue)
{
((LanguageProperty)obj).Values[cultureName] = newValue;
Save(((LanguageProperty)obj).Key, cultureName, newValue);
}
},
typeof(string)),
PropertyInfoAccessorFactory.CreateInpcPropertyAccessor)
.Build()),
IsReadOnly = false
});
foreach (var column in propertyColumns)
{
_languagePropertyDataGrid.Columns.Add(column);
} This code demonstrates how to bind both fixed and dynamic columns and provides the logic for getting the values to display and setting the values for modification. However, in actual use, it doesn't necessarily have to be defined as a dictionary. As long as you can loop through and bind the dynamic columns and provide the necessary logic for getting and setting the values, it should work. You might find it beneficial to explore the source code of this project to address your specific issue. Best regards, Henry |
Beta Was this translation helpful? Give feedback.
-
Hi Henry, Thanks for your reply, I'll have a looksee when I get a moment, I'll have to dig out my project I was going to use this for Thanks again Jake |
Beta Was this translation helpful? Give feedback.
-
Hi,
I'm trying to get Avalonia working with a project of mine but have run into a issue. I'm importing a csv file of any number of columns to edited with in the program.
I've been able to get a example working - https://www.nequalsonelifestyle.com/2019/06/13/avalonia-datagrid-getting-started/ with a class and have been able to bind a datatable. However since the Datatable is uneditable on testing. I tried to create a dynamic class to capture and bind. However the Data will not show is there a process to attach non-uniform data to the grid? This is my first project with Avalonia but have worked with WPF in the past.
if it helps to see my code the whole project is here https://github.com/JakeTrans/CSVEditor/tree/AvaloniaUI (CSVEditorForm is the Avalonia Attempt).
Thanks
Jake
Beta Was this translation helpful? Give feedback.
All reactions