简体   繁体   English

绑定数据网格

[英]Binding datagrid

I've been trying to bind DataGridTextColumns column-wise to arrays (int[] I think) and have been unsuccessful. 我一直在尝试将DataGridTextColumns按列方式绑定到数组(我认为是int []),但没有成功。 Nothing I've found on msdn, stackoverflow etc has been particularly helpful . 我在msdn,stackoverflow等上都找不到特别有用的东西。

<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>

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). 我已经尝试了很多方法,包括使用datagrid或datagridtextcolumns(itemsource等)进行绑定,在代码中定义datacontext(出于mvvm的原因,我希望尽可能地避免使用代码),使用路径和源代码,并尝试将我的两个数组组合到一个类中(无论如何我都不是真正想要的)。 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 };

Anyone got any ideas? 任何人有任何想法吗?

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. 无论如何,这是许多方式中的一种,因此,如果您愿意,可以发布另一个答案。

For a start you cannot bind to a private field, you need to make them a public property: 首先,您不能绑定到私有字段,需要将它们设置为公共属性:

private int[] _slaveAddressValues = { 0, 0, 0, 0 };

public int[] SlaveAddress 
{
    get { return _slaveAddressValues; }
    set { _slaveAddressValues = value; }
}

(Note that I've omitted the property setting notification.) (请注意,我省略了属性设置通知。)

The second problem is that you shouldn't bind a DataColumn to an independant property of the inherited data context like this - it needs to be bound to a property of the item contained in the IEnumerable that you've bound to the ItemsSource of the grid . 第二个问题是,您不应像这样将DataColumn绑定到继承的数据上下文的独立属性-它需要绑定到已绑定到网格ItemsSource 的IEnumerable包含的项目的属性。 Here is another sample . 这是另一个示例

Hopefully these links will be of some help, they should at least get you started. 希望这些链接会有所帮助,至少应该使您入门。

Need to bind to a single source 需要绑定到一个来源

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

create 创造

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

Bind the DataGrid to Adddress 将DataGrid绑定到地址

Then on the columns bind the path to Slave and Meter 然后在列上将路径绑定到Slave和Meter

Right, thanks for your answers. 是的,谢谢您的回答。 Here's how I solved it in the end: 这是我最终解决的方法:

public List<Address> Addresses = new List<Address>();

in my main class 在我的主要班级

dataGrid1.DataContext = Addresses;

in entry method to main class 在进入主类的方法中

for(int i=0; i<10; i++)
{
    Addresses.Add(new Address(){Meter = "",Slave = i});
}

for (int i = 0; i < 10; i++)
{
    Addresses[i].Meter = Convert.ToString(i);
}

in my entry method to show i can easily populate and edit this list 在我的输入方法中显示我可以轻松填充和编辑此列表

   public class Address
{
    public int Slave { get; set; }
    public string Meter { get; set; }

    public Address()
    {
    }
}

and this above as the extra class I needed obviously 这显然是我需要的额外课程

with: 有:

<DataGrid Name="dataGrid1" ItemsSource="{Binding}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Slave Address" Binding="{Binding Path=Slave}"/>
                <DataGridTextColumn Header="Meter Readings" Binding="{Binding Path=Meter}"/>
            </DataGrid.Columns>
        </DataGrid>

WPF introduces a very helpful thing to you when you don't have any way to display things into view for your object. 当您无法以任何方式在对象中显示事物时,WPF会为您带来非常有用的东西。 you can use converters. 您可以使用转换器。 I am sure this Converter system will help you to fine the way you want. 我确信此Converter系统将帮助您完善所需的方式。

Create a Converter which takes an array and return something like string of concatenated items or may be some thing you want. 创建一个采用数组的Converter,并返回类似串联项的字符串之类的东西,或者可能是您想要的东西。 and also do Convert Back From the display value to object. 并将从显示值转换回对象。 for synchronization in both side. 双方同步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM