I am creating a little chat demo, by having two chat boxes next to each other (they communicate to each other). My issue is, when I load older messages from the database, I could not properly set a different "current author" for each chat box while maintaining MVVM structure (what I need is: in chatbox 1 user1 is the currentAuthor, in chatbox 2 user 2 is the currnetAuthor)
I am following an MVVM pattern. Code snippets are provided below (I tried to remove all lines irrelevant to the question):
1- my chat boxes
<telerik:RadChat x:Name="chat1"
Width="400"
Height="550"
DataSource="{Binding Chat1Messages}"
CurrentAuthor="{Binding Chat1CurrentAuthor}">
</telerik:RadChat>
<telerik:RadChat x:Name="chat2"
Width="400"
Height="550"
DataSource="{Binding Chat2Messages}"
CurrentAuthor="{Binding Chat2CurrentAuthor}">
</telerik:RadChat>
2- My code in adding the messages from DB:
//some code to get data from DB//
this.chat1CurrentAuthor = this.FirstAuthor;
this.chat2CurrentAuthor = this.SecondAuthor;
//Iterator over data from DB
{
//if message is written by first author:
{
this.Chat1Messages.Add(new TextMessageObject() { Text = row.MessageText, MsgAuthor = this.FirstAuthor, CreationDate = row.Time });
this.Chat2Messages.Add(new TextMessageObject() { Text = row.MessageText, MsgAuthor = this.FirstAuthor, CreationDate = row.Time });
}
//if message is written by second author:
{
this.Chat1Messages.Add(new TextMessageObject() { Text = row.MessageText, MsgAuthor = this.SecondAuthor, CreationDate = row.Time });
this.Chat2Messages.Add(new TextMessageObject() { Text = row.MessageText, MsgAuthor = this.SecondAuthor, CreationDate = row.Time });
}
}
3- code to support MVVM:
public Author Chat1CurrentAuthor
{
get { return this.chat1CurrentAuthor; }
set
{
if (value != this.chat1CurrentAuthor)
{
this.chat1CurrentAuthor = value;
OnPropertyChanged(() => this.chat1CurrentAuthor);
}
}
}
public Author Chat2CurrentAuthor
{
get { return this.chat2CurrentAuthor; }
set
{
if (value != this.chat2CurrentAuthor)
{
this.chat2CurrentAuthor = value;
OnPropertyChanged(() => this.chat2CurrentAuthor);
}
}
}
With my current code, both authors are set up as current authors in both chatboxes (i.e in chatbox1, currentAuthor is both user1 and user2, in chatbox2 currentAuthor is both user1 and user2) which is not the desired behavior.