简体   繁体   中英

Binding datagrid

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>

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

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

Then on the columns bind the path to Slave and 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. you can use converters. I am sure this Converter system will help you to fine the way you want.

Create a Converter which takes an array and return something like string of concatenated items or may be some thing you want. and also do Convert Back From the display value to object. for synchronization in both side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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