K
K
kodwi2014-07-24 15:25:59
WPF
kodwi, 2014-07-24 15:25:59

How to implement custom data binding to DataGrid in WPF?

There is a class that contains a property, where the list element is an element read from the file (an element is an array of fields, each of which contains a value in the form of an array of bytes, the number of fields for all elements is the same), in the form of a two-dimensional array a[][] , where i is the number of the element field, a[i] is the value of the i-th field as an array of bytes. Field bytes are interpreted by different types (the bytes of the string are stored somewhere, the bytes of a real number are stored somewhere, etc.), and the corresponding functions are implemented to convert from bytes to a string and from a string to bytes, taking into account the type of the field. I need to implement output / editing / adding / deleting elements from the property of an object of a given class in a DataGrid. Element fields should not be represented by columns, but by rows.List<byte[][]>
List<byte[][]>
I am aware of the standard binding to the DataGrid by creating a separate class and screwing its ObservableCollection to the DataGrid, but my list of elements that need to be displayed in the DataGrid lies inside one object, and the elements are not class objects, but two-dimensional arrays.
As far as I know, without binding, it is impossible to operate purely programmatically on events with actions on DataGrid rows. Is it possible to somehow work differently through the DataGrid with my data in the current form? If so, how?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sumor, 2014-07-24
@Sumor

I propose to use not a datagrid, but an ItemsControl to display a list of your parameters.
The outer ItemsControl enumerates a class property of type List, the second ItemsControl is an element of this list of type byte[][], the third is byte[] and displays a TextBox for each digit.
By binding (Binding) you change the values ​​in the text boxes, you change it in the property of your object.
xml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ItemsControl x:Name="itemsData">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ItemsControl BorderBrush="Black" BorderThickness="1" ItemsSource="{Binding}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <ItemsControl BorderBrush="Black" BorderThickness="1" ItemsSource="{Binding}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <StackPanel  Orientation="Horizontal"/>
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <TextBox Text="{Binding Path=.}" />
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

C#
namespace WpfApplication1
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var my = new MyClass();
            my.Data = new List<byte[][]>();
            my.Data.Add(new byte[][] { new byte[] { 0, 1 }, new byte[] { 2, 3 }, new byte[] { 4, 5 } });
            my.Data.Add(new byte[][] { new byte[] { 6, 7 }, new byte[] { 8, 9 }, new byte[] { 10, 11 } });
            itemsData.ItemsSource = my.Data;
        }

    }

    public class MyClass
    {
        public List<byte[][]> Data
        { get; set; }
    }
}

P
p1x, 2014-07-31
@p1x

It seems to me that it is best to create a ViewModel that is bound to the DataGrid on the one hand, and synchronized with your collection on the other. From alternative options, I can suggest trying to use ListView with GridView or DataGrid.CellEditEnding and DataGrid.RowEditEnding events .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question