1

EDIT: I'm open to another approach. The source data is XML, but there's surely other ways to get the datagrid behaving.

This answer got me started, but things seem to be different within a DataGrid. The code works fine if I leave out the value converter, but if I put in the value converter, all I receive as input are empty strings.

Good but imperfect:

    <DataGridTextColumn 
                Header="Length" 
                Binding="{Binding XPath=length/@value}}" />

Junk:

    <DataGridTextColumn 
                Header="Length" 
                Binding="{Binding XPath=length, 
                          Converter={StaticResource valueFormattingConverter }}" />

I have a theory that the above is giving me the element content for element ./length (it's an empty element), but I actually want the element ./length because the attributes need to be processed.

Good but for combo boxes:

<ComboBox Style="{StaticResource ComboButtonStyle}" Width="200" 
        Text="{Binding Path=., Converter={StaticResource valueFormattingConverter }}"
          IsEditable="True" />

So with the Combo example, valueFormattingConverter.Convert gets called with an XmlElement (yay!). With the DataGridTextColumn example, it's called with an empty string.

EDIT: ongoing research points to IMultiValueConverter as something I might want.

Community
  • 1
  • 1
Alan Baljeu
  • 2,383
  • 4
  • 25
  • 40

1 Answers1

0

This works, but is tedious. I need to remake everything between DataGridTextColumn.Binding in every column, changing up each time the XPath.

If anybody can come up with a better way, the prize is open.

  <W3V:UnitsConverter x:Key="unitsConverter" />

...

    <DataGridTextColumn Header="Length">
      <DataGridTextColumn.Binding>
        <MultiBinding Converter="{StaticResource unitsConverter}">
          <Binding XPath="length/@value"/>
          <Binding XPath="length/@type"/>
          <Binding XPath="length/@units"/>
        </MultiBinding>
      </DataGridTextColumn.Binding>
    </DataGridTextColumn>

...

[ValueConversion(typeof(XmlElement), typeof(string))]
public class UnitsConverter : IMultiValueConverter
{

object IMultiValueConverter.Convert(object[] value, Type targetType, 
                   object parameter, System.Globalization.CultureInfo culture)
{
  if (value == null) return new List<string>();

  string data = value[0] as string;
  string type = value[1] as string;
  string unit = value[2] as string;

  return Information.FormatValue(data, type, unit);

}
....
}
Alan Baljeu
  • 2,383
  • 4
  • 25
  • 40