Avalonia DataGrid and object[] how to tie

102 Views Asked by At

There is

public object[]? obj;
public ObservableCollection<object>datatablew { get; set; }
public diztablew(object grps)
{
    datatablew = new ObservableCollection<object>() { grps };
    obj = new object[3];
    obj[0] = "1111";
    datatablew.Add(obj);
}
 how to tie
 <DataGridTextColumn Header="id" Binding="{Binding Path=?????}"/>

is it possible ? class grps not available at this location .

2

There are 2 best solutions below

3
On BEST ANSWER

I solved part of the problem like this

public void init(object grps, Microsoft.Data.SqlClient.SqlConnection conns)
        {
            var testAssembly = Assembly.LoadFrom(grps.GetType().Assembly.FullName?.Split(",")[0] + ".dll");
            var t = testAssembly.GetType(grps.GetType().FullName);
            datatablew = new ObservableCollection<object>();
            string cluc = "select top 10 kod_rel,uet,rtrim(name) as name from statistika.dbo.statpra";
            var comms = new Microsoft.Data.SqlClient.SqlCommand(cluc, conns);
            var readers = comms.ExecuteReader();
            var counts = readers.FieldCount;
            while (readers.Read() == true)
            {
                var ooo = Activator.CreateInstance(t);
                for (int i = 0; i < counts; i++)
                {
                    var obbjj = readers.GetValue(i);
                    try
                    {
                        var pps = grps.GetType().GetProperty(readers.GetName(i));
                        pps.SetValue(ooo,obbjj,null); 
                    }
                    catch { }
                }
                datatablew.Add(ooo);
            }
            readers.Close();
        }    
it remains to be decided
<DataGridTextColumn Header="kod_rel" Binding="{Binding ????,StringFormat=0;-0;#}"/>
`, how to bind kod_rel which is in ObservableCollection<object> , if I bet ```AutoGenerateColumns="true"``` 
then everything works as it should, but I need to define the columns myself .`
2
On

Don't just use object as your data type. Make a view model class with properties. Such as:

public class MyDataObjectViewModel : ReactiveObject
{
   private string _someValueProperty
   public string SomeValueProperty
   {
        get => _someValueProperty;
        set => this.RaiseAndSetIfChanged(ref _someValueProperty, value);
   }
}

In your view model for the axaml page then expose a property that contains your objects, like:

public class MainWindowViewModel : ReactiveObject
{
    private ObservableCollection<MyDataObject> _myCollection
    public ObservableCollection<MyDataObject> MyCollection
    {
        get => _myCollection;
        set => this.RaiseAndSetIfChanged(ref _myCollection, value);
    }
}

Use data binding to attach the collection to an instance of a DataGrid.

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:AvaloniaApplication1.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="AvaloniaApplication1.Views.MainWindow"
        x:DataType="vm:MainWindowViewModel"
        Icon="/Assets/avalonia-logo.ico"
        Title="AvaloniaApplication1">

    <Design.DataContext>
        <!-- This only sets the DataContext for the previewer in an IDE,
             to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
        <vm:MainWindowViewModel/>
    </Design.DataContext>

    <DataGrid Items="{Binding MyCollection}" />

</Window>