I’m writing a simple WPF interface to retrieve SwiftMessages using a library component. I cannot change the component. I have a DataGrid that should show the messages, one message per row.
My only use of code-behind is to instantiate the DataContext (so yes dear MVVM absolutionists, it’s not 100% MVVM, but please be nice and useful as I’m learning):
public SwiftSearchWindow()
{
InitializeComponent();
DataContext = new UI.ViewModels.SwiftSearchViewModel();
}
The relevant XAML is thus:
<DataGrid Name="gridMessages" ItemsSource="{Binding SwiftMessages}">
<DataGrid.Columns>
<DataGridTextColumn Header="Message Type" Binding="{Binding MessageType, Mode=OneWay}" ></DataGridTextColumn>
<DataGridTextColumn Header="Message Length" Binding="{Binding MessageLength, Mode=OneWay}"></DataGridTextColumn>
<DataGridTextColumn Header="Counter Party" Binding="{Binding CounterParty, Mode=OneWay}"></DataGridTextColumn>
<DataGridTextColumn Header="Direction" Binding="{Binding Direction, Mode=OneWay}"></DataGridTextColumn>
<DataGridTextColumn Header="Message Date" Binding="{Binding MessageDate, Mode=OneWay}"></DataGridTextColumn>
<DataGridTextColumn Header="Network Interface Message Reference" Binding="{Binding NetworkInterfaceMessageReference, Mode=OneWay}"/>
<DataGridTextColumn Header="Own Bic" Binding="{Binding OwnBic, Mode=OneWay}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
On my instance of SwiftSearchViewModel is a property:
public SwiftMessages SwiftMessages
{
get
{
if (_query == null) return null;
return _query?.Messages;
}
}
Relevant parts of SwiftMessages are:
public class SwiftMessages : IEnumerable, IEnumerator
{
private List<SwiftMessage> _items;
}
…and relevant parts of SwiftMessage are below. I’ve omitted some of the properties, but they’re all strings, except for MessageLength:
public class SwiftMessage
{
public int MessageLength
{
get { return _messageLength; }
set { _messageLength = value; }
}
public string MessageType
{
get { return _messageType; }
set { _messageType = value; }
}
}
Two messages are retrieved and I get two rows shown in the DataGrid. However, the individual cell values do not bind. I get these errors:
System.Windows.Data Error: 40 : BindingExpression path error: 'MessageType' property not found on 'object' ''Int32' (HashCode=1)'. BindingExpression:Path=MessageType; DataItem='Int32' (HashCode=1); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'MessageLength' property not found on 'object' ''Int32' (HashCode=1)'. BindingExpression:Path=MessageLength; DataItem='Int32' (HashCode=1); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'CounterParty' property not found on 'object' ''Int32' (HashCode=1)'. BindingExpression:Path=CounterParty; DataItem='Int32' (HashCode=1); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'Direction' property not found on 'object' ''Int32' (HashCode=1)'. BindingExpression:Path=Direction; DataItem='Int32' (HashCode=1); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'MessageDate' property not found on 'object' ''Int32' (HashCode=1)'. BindingExpression:Path=MessageDate; DataItem='Int32' (HashCode=1); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'NetworkInterfaceMessageReference' property not found on 'object' ''Int32' (HashCode=1)'. BindingExpression:Path=NetworkInterfaceMessageReference; DataItem='Int32' (HashCode=1); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'OwnBic' property not found on 'object' ''Int32' (HashCode=1)'. BindingExpression:Path=OwnBic; DataItem='Int32' (HashCode=1); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'MessageType' property not found on 'object' ''Int32' (HashCode=2)'. BindingExpression:Path=MessageType; DataItem='Int32' (HashCode=2); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'MessageLength' property not found on 'object' ''Int32' (HashCode=2)'. BindingExpression:Path=MessageLength; DataItem='Int32' (HashCode=2); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'CounterParty' property not found on 'object' ''Int32' (HashCode=2)'. BindingExpression:Path=CounterParty; DataItem='Int32' (HashCode=2); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'Direction' property not found on 'object' ''Int32' (HashCode=2)'. BindingExpression:Path=Direction; DataItem='Int32' (HashCode=2); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'MessageDate' property not found on 'object' ''Int32' (HashCode=2)'. BindingExpression:Path=MessageDate; DataItem='Int32' (HashCode=2); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'NetworkInterfaceMessageReference' property not found on 'object' ''Int32' (HashCode=2)'. BindingExpression:Path=NetworkInterfaceMessageReference; DataItem='Int32' (HashCode=2); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'OwnBic' property not found on 'object' ''Int32' (HashCode=2)'. BindingExpression:Path=OwnBic; DataItem='Int32' (HashCode=2); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
My questions are:
- What Int32 is it trying to bind to?
- I’m aware that I’m simply using an IEnumerable from a C# library and trying to bind to it. I consider the data generated by the library to be my Model. I’ve seen other examples where the model constructs ObservableCollections etc (not possible in this case) to feed the UI, but surely that breaks the concept that the Model should work independantly of the UI.
- How do I fix this so that the properties show?
- Lastly, I have a few other properties of SwiftMessage that are string[]. I’d like to concatenate the array into a single CSV string and show that in a cell. How do I slot this calculation inbetween the XAML and the model data?