绑定datagrid [英] Binding datagrid

查看:176
本文介绍了绑定datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将DataGridTextColumns列列化为数组(int []我认为),并且一直不成功。没有什么我发现在msdn,stackoverflow等一直是特别有用的。

I've been trying to bind DataGridTextColumns column-wise to arrays (int[] I think) and have been unsuccessful. Nothing I've found on msdn, stackoverflow etc has been particularly helpful .

<DataGrid AutoGenerateColumns="False" VerticalAlignment="Top" Margin="0,47,0,0" Height="351" Name="dataGrid1" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Slave Address" Width="100" Binding="{Binding SlaveAddressValues}"/>
        <DataGridTextColumn Header="Meter Readings" Width="100" Binding="{Binding MeterReadingValues}"/>
    </DataGrid.Columns>
</DataGrid>

我尝试过一些事情,包括使用datagrid或datagridtextcolumns项目来源等),在代码中定义数据文本(我想尽可能保持mvvm原因的代码),使用路径和源,并尝试将我的两个数组组合在一个类中(这不是真的我想反正)。然而,甚至没有任何工作可以获得以下简单的测试用例,以正确绑定datagrid。

I've tried a number of things including putting binding in with datagrid instead or as well as datagridtextcolumns (itemsource etc), defining datacontext in code (I'd like to keep as much as possible out of code for mvvm reasons), using paths and sources, and also trying to combine my two arrays in a class (which isn't really what I want anyway). However nothing has worked in getting even the following simple test case to properly bind with the datagrid.

private int[] SlaveAddressValues = { 0, 0, 0, 0 };
private int[] MeterAddressValues = { 2, 2, 2, 3 };

任何人都有任何想法?

,也许我应该把我的解决方案放在这里,但这是一个很长的时间,所以我把它作为一个答案。无论如何,这是很多方法之一,如果你感觉如此倾向,请发布另一个答案。

Right, maybe I should have put my solution here but it was a bit long so I put it as an answer. it's one of many ways anyway so post another answer if you feel so inclined.

推荐答案

需要绑定到单个来源



Need to bind to a single source

class  Address
{
    public Int Slave { get; set; }
    public Int Meter { get; set; }
}

创建

Public List<Address>  Addresses { get; set; }

将DataGrid绑定到Adddress

Bind the DataGrid to Adddress

然后在列上绑定到Slave和Meter

Then on the columns bind the path to Slave and Meter

这篇关于绑定datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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