Mark Denom
Mark Denom

Reputation: 1057

Why is my view not updating when adding data from my service to my viewmodel?

So I just created a new project and I am trying to add some data to the collection in my ViewModel, however when I add data to it, it does add but it wont update the UI.

This is where I set the DataContext and where I am trying to add some content to the collection

ProxyService ps;

public MainWindow()
{
    InitializeComponent();
    DataContext = new BaseViewModel();

    ps = new ProxyService();
    ps.AcceptConnection();
}

Keep in mind, it does add it to the collection there are no errors I've debugged it and it's infact in the collection. ProxyServer.cs

public class ProxyService : MessageViewModel
{
    public void AcceptConnection()
    {
        Messages.Add(new MessageModel { Message = "Awaiting connection..." });

Here is the BaseViewModel

public class BaseViewModel
{
    public MessageViewModel MessageViewModel { get; set; } = new MessageViewModel();
}

And the MessageViewModel of course

public class MessageViewModel : ObservableObject
{
    private ObservableCollection<MessageModel> _messages;

    public ObservableCollection<MessageModel> Messages
    {
        get { return _messages; }
        set
        {
            _messages = value;
            OnPropertyChanged();
        }
    }


    public MessageViewModel()
    {
        Messages = new ObservableCollection<MessageModel>();
    }
}

And here is the XAML for the ScrolLViewer to which I am adding the data

<ScrollViewer  Height="380"
                           Margin="10"
                           >
    <StackPanel>
        <ItemsControl ItemsSource="{Binding MessageViewModel.Messages}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock FontFamily="Consolas" 
                                           Foreground="#61d73d"
                                           Text="{Binding Message}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>

</ScrollViewer>

The issue I am facing is that it's not updating the UI when it adds something However! If I add something in the constructor it works just fine. As an example this works just fine, it shows it in the view accoringly

public MessageViewModel()
{
    Messages = new ObservableCollection<MessageModel>();
    Messages.Add(new MessageModel { Message = "Hello World!" });
}

My best guess is that it's adding to another instance of some sort but I am not sure, I really don't want to have to use singleton because I feel like that will ruin the MVVM pattern.

Upvotes: 0

Views: 360

Answers (2)

mm8
mm8

Reputation: 169240

BaseViewModel is one class and ProxyService another one. You can't expect MessageModel objects that you add to the latter affect the former and vice versa. Try to set the DataContext to a BaseViewModel:

public MainWindow()
{
    InitializeComponent();
    ps = new ProxyService();
    ps.AcceptConnection();
    DataContext = new BaseViewModel { MessageViewModel = ps };
}

Upvotes: 1

Peregrine
Peregrine

Reputation: 4546

Three points of note.

  1. You're setting the Window's DataContext to one instance of BaseViewModel, and then creating a separate instance of the ProxyServiceClass.

  2. Your binding for the ItemsControl.ItemsSource should just be binding to a property of the DataContext, in this case Messages.

  3. Don't keep recreating the ObservableCollection - just create it once and add / remove items as required. Any bound control will detect that it implements INotifyCollectionChanged and refresh itself automatically when the collection is updated.

Upvotes: 1

Related Questions