数据网格中对象的矩阵表示 [英] Matrix presentation of objects in datagrid

查看:95
本文介绍了数据网格中对象的矩阵表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将对象列表表示为矩阵。矩阵的大小始终为16x16。

I need to represent a list of objects as a matrix. The size of the matrix is always 16x16.

对象仅包含3个字段:

private byte deviceAddress;
private byte register;
private byte[] data;

在每个单元格内仅应显示 data 字段的值,单元格应为可更新

Inside each cell should be displayed only value of data field and also each cell should be updatable.

我尝试用 new DataTable 作为返回对象来实现一些转换器,但是每次执行转换时, datagrid闪烁并绘制新的列和行(这是不可接受的)。

I tried to implement some converter with new DataTable as returned object, but each time when I perform the conversion, the datagrid flickers and draws new columns and rows (this is not acceptable).

也许存在某种写数据模板来表示这种情况的方法?

Maybe exist some way to write datatemplate to represent this?

预期结果应如图所示:

推荐答案

假设您的数据存储在这样的类中:

Supposing that your data is stored in a class like this:

public class MyItem
{
    public byte deviceAddress { get; set; }
    public byte register { get; set; }
    public byte[] data { get; set; }
}

,您有256个此类对象的列表

and you have a list of 256 such objects

public List<MyItem> MyMatrix { get; set; }

您可以将 ItemsControl UniformGrid 作为ItemsPanel:

you could use an ItemsControl with a UniformGrid as ItemsPanel:

<ItemsControl ItemsSource="{Binding MyMatrix}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="16"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding data,
                     Converter={StaticResource yourDataToStringConverter}}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

如果需要选择一个项目,请使用ListBox而不是ItemsControl。

If you need to be able to select an item, use a ListBox instead of an ItemsControl.

请注意,必须将Window的DataContext设置为具有 MyMatrix 属性的类的实例,并且需要实现IValueConverter的功能,它将 data 转换为适当的字符串表示形式(或其他任何形式)。

Note that the Window's DataContext must be set to an instance of a class with the MyMatrix property, and you need an implementation of IValueConverter that converts data to an appropriate string representation (or whatever else).

这篇关于数据网格中对象的矩阵表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆