That is a DataBinding Error
Easiest way to read those is break it up by the colons/semi-colons, and read it backwards
System.Windows.Data Error: 40 : BindingExpression path error: 'Sender'
property not found on 'object' ''Char' (HashCode=6619237)'.
BindingExpression:Path=Sender; DataItem='Char' (HashCode=6619237);
target element is 'TextBlock' (Name=''); target property is 'Text'
(type 'String')
- target property is 'Text' (type 'String')
- target element is 'TextBlock' (Name='');
- BindingExpression:Path=Sender;
- DataItem='Char' (HashCode=6619237);
- 'Sender' property not found on 'object' ''Char' (HashCode=6619237)'.
- BindingExpression path error:
- System.Windows.Data Error: 40 :
1 tells you that there is a Text
property causing the error
2 tells you that the Text property is on a <TextBlock>
element
3 tells you the binding express causing the problem is {Binding Path=Sender}
4 tells you the DataItem/DataContext behind the <TextBlock>
element is an item of data type Char
5 tells you the actual problem with this: there is no property named Sender
on the object of type Char
6 just tells you it's a binding error
7 I have no idea what it means
Since I see you have a public property named Sender
on your Message
class, and its clear that Message
is not Char
, its obvious that your DataContext
for each item is wrong.
Since it is set to a Char
the most likely cause is you are binding to a string, and the DataContext
for each element is a character in that string.
And sure enough, ItemsSource="{Binding Source=Messages}
means you are changing the binding's Source
property from the current DataContext
to a string
. And strings are just character arrays, so it means you are binding to the character array { M, e, s, s, a, g, e, s }
If you change the Source
property to the Path
property, then it will correctly read DataContext.Messages
instead, and should work.
<ListView ItemsSource="{Binding Path=Messages}" ... />
(The word Path
here is optional, since if you don't specify a property name then the binding assumes it is the value for the Path
property)
As a side note, I don't see you setting your
DataContext
anywhere on the form, and I don't see a public
Messages
property either.
The MainWindow
constructor should probably have a line of code that looks like this to set the DataContext
to itself :
this.DataContext = this;
And you probably need a public property for your ObservableCollection<Message> messages
so the ListView
binding can find it :
public ObservableCollection<Message> Messages
{
get { return messages; }
set { messages = value; }
}
I'm not sure if these were merely overlooked, or if you didn't know you needed them.
Oh, and if you plan on changing any of these bound properties and having the UI automatically update, you'll want to implement INotifyPropertyChanged too :)
And since I'm in tutorial-mode here, I figured I should also link to this answer :
Transitioning from Windows Forms to WPF
I would highly recommend reading through it (and the linked articles) if you're new to how WPF works, and are switching from Winforms to WPF. Which it sounds like you are :)