basilard99
basilard99

Reputation: 307

How to pass data to a viewmodel in Caliburn.Micro

This is probably a very simple question, but at this time I have myself so confused I can't see the answer. Simply put, I have a window that contains a content control. I'm using Caliburn.Micro's conventions to "locate" the view.

The window looks like this:

<Window x:Class="Views.MainWindowView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox/>
        <ContentControl x:Name="MyViewModel" Height="Auto" Background="Blue"/>
    </Grid>
</Window>

The view itself is successfully found, and the screen displays as I expected. However, MyViewModel needs to make a service call to get information based on what is typed into the text box.

So, what I can't seem to figure out is how one would pass that information from the text box to the view model. I've thought of several options, but they all seem to be too much work which makes me think that I'm missing something simple.

Thanks a lot

Upvotes: 1

Views: 1957

Answers (1)

Derek Beattie
Derek Beattie

Reputation: 9478

Like you said there are a number of things you can do:

  1. You could expose a property on MyViewModel and set it within MainWindowView.

  2. You could use the EventAgregator, publish an event from the MainWindowView and subscribe to that event from MyViewModel.

  3. Using MEF you could inject a shared resource between the two ViewModels, set it in MainWindowViewModel, and be able to access it from MyViewModel.

Upvotes: 2

Related Questions