简体   繁体   中英

Custom WPF datagrid header and xml binding

I've been trying to figure out how to recreate this "complex" datagrid header, binded to an XML file. I haven't found any examples about how to tackle this problem.

This is an example XML data file (which can have more 'sensors' and more 'Measures'):

<?xml version="1.0" encoding="utf-8" ?>
<ShipData>
  <Draught>
    <Measures>
      <Mdata>M1</Mdata>
      <Mdata>M2</Mdata>
    </Measures>
    <Sensor Name="Sensor1">
      <Measurement>
        <Bis>45</Bis>
        <Man>43</Man>
      </Measurement>
      <Measurement>
        <Bis>44</Bis>
        <Man>46</Man>
      </Measurement>
    </Sensor>
    <Sensor Name="Sensor2">
      <Measurement>
        <Bis>45</Bis>
        <Man>43</Man>
      </Measurement>
      <Measurement>
        <Bis>43</Bis>
        <Man>45</Man>
      </Measurement>
    </Sensor>
  </Draught>
</ShipData>

And this is how I would style my datagrid:

带有自定义双行标题的datagrid

Since the data can change (more sensors, and more measures), I'll be doing some code behind loading of the datagrid.

Edit: So the things I'm trying to do is:

  1. Create the header (consisting of two lines, the upper line spanning two columns)

    (solved) >> Since you can't really span multiple columns for the header, the best way is to create a grid, just above the Datagrid with equal possible solution

    You could play with:

     <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.HeaderTemplate> <DataTemplate> ... 

    ... but it get's ugly...at least here. I don't think the HeaderTemplate is used for the things I want to do.

  2. Bind the xml data to the grid

    Still figuring out (and how to save changes to the data back to the file)

Preferably via code behind

I solved the problem about the "super" header (see above for example):

Since you can't really span multiple columns for the header, the best way is to create a grid, just above the Datagrid with equal column sizes (or combined sizes for the super header)

I'm stil struggling with the code behind xmlprovider and xpath (but I'll make a new question to not pollute this question any further)

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