开发者

Simple WPF binding question. Binding DataGrid to one structure and array(list) of structures

I want to display the contents of a binary file in a few simple DataGrids (xmlns:kit="http://schemas.microsoft.com/wpf/2008/toolkit"). I know the layout of the file; it's quite simple.

First I have several simple structs of the form:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class PartitionBasicStationData
{
  public uint longitude;
  public uint latitude;
  public uint StationCode;

}

I have just one intance of these in the file. Although it's probably overkill, I'd like to throw it in a Datagrid. I was hoping I'd automatically get some simple column headers like, "longitude", "latitude", etc. My XAML looks like:

<kit:DataGrid Name="m_gridPartion1" MinWidth="120" MinHeight="120"></kit:DataGrid>

and in code, after I successfully read the binary file and create a structure, I do this:

m_gridPartion1.ItemsSource = new Partition1[1] { partition1 };
//m_gridPartion1.DataContext = new Partition1[1]{partition1}; // also tried this
// m_gridPartion1.ItemsSource = partion1; // and I tried this

Should this work?

For my second grid I will also have simple structures, but many of them. The structure looks like

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class开发者_如何转开发 Datum
{
  public uint temperature;
  public uint windVelocity;

}

I was hoping I could just do something like this:

 m_gridData.ItemsSource = myList;  // myList is List<Datum>

Any tutorials or pointers would be much appreciated. Most of the material I've come across is about binding from XAML (which I'm not opposed to if it's easy) or is overkill (setting column names, etc.).

Thanks,

Dave


Define your class as

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class PartitionBasicData
{
    public uint longitude { get; set; }
    public uint latitude { get; set; }
    public uint StationCode { get; set; }
}

and set the AutoGenerateColumns property of the DataGrid to True (if you don't want to define the columns manually)

With these changes, i was able to get the following code to work:

m_gridPartition1.ItemsSource = new PartitionBasicData[] { new PartitionBasicData() { latitude = 1, longitude = 2, StationCode = 3 }};


Edit for question asked in comment:

In this case you will need to remove the AutoGenerateColumns attribute and define your columns manually (as you will be specifying a custom column for the StationCode property)

For the StationCode column, you will then need to specify a Converter to format your array, or alternatively if the byte[] is always in a known length and format you could just specify the StringFormat property of the Binding.

Both methods would require you to either set the CanUserSort property on that column to False, or set the SortMemberPath property to something that doesn't require converting so that a run-time exception is not thrown when the user clicks the column header. Or since there is only ever one row to this DataGrid as specified in your original question, you could set the CanUserSortColumns property on the DataGrid to False.

Converter method:

<DataGrid Name="m_gridPartition1">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Longitude" Binding="{Binding Path=longitude}" />
        <DataGridTextColumn Header="Latitude" Binding="{Binding Path=latitude}" />
        <DataGridTextColumn Header="Station Code" Binding="{Binding Path=StationCode, Converter={StaticResource someConverterToStyliseByteArray}}" CanUserSort="False" />
    </DataGrid.Columns>
</DataGrid>

StringFormat method:

<DataGridTextColumn Header="Station Code" Binding="{Binding Path=StationCode, StringFormat='{}{0}-{1}-{2}'}" CanUserSort="False" />


Expose the fields as public properties and it might work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜