views:

24

answers:

2

Okay guys, I'm new to this model and Silverlight in general. I have the following code (changed object names, so syntax/spelling errors ignore).

public class ViewModel
{
    ViewModelSource m_vSource;

    public ViewModel(IViewModelSource source)
    {
        m_vSource= source;
        m_vSource.ItemArrived += new Action<Item>(m_vSource_ItemArrived);
    }

    void m_vSource_ItemArrived(Item obj)
    {
        Title = obj.Title;
        Subitems = obj.items;
        Description = obj.Description;
    }

    public void GetFeed(string serviceUrl)
    {
        m_vFeedSource.GetFeed(serviceUrl);
    }

    public string Title { get; set; }
    public IEnumerable<Subitems> Subitems { get; set; }
    public string Description { get; set; }
 }

Here is the code I have in my page's codebehind.

ViewModel m_vViewModel;

public MainPage()
{
    InitializeComponent();

    m_vViewModel = new ViewModel(new ViewModelSource());
    this.Loaded += new RoutedEventHandler(MainPage_Loaded);

    this.DataContext = m_vViewModel;
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    m_vViewModel.GetItems("http://www.myserviceurl.com");
}

Finally, here is a sample of what my xaml looks like.

<!--TitleGrid is the name of the application and page title-->
<Grid x:Name="TitleGrid" Grid.Row="0">
    <TextBlock Text="My Super Title" x:Name="textBlockPageTitle" Style="{StaticResource PhoneTextPageTitle1Style}"/>
    <TextBlock Text="{Binding Path=Title}" x:Name="textBlockListTitle" Style="{StaticResource PhoneTextPageTitle2Style}"/>
</Grid>

I know I'm missing something, but I'm just not knowledgable enough which is why I'm asking you guys :) Is there anything I'm doing wrong here?

Thanks!

A: 

Well, go figure, 10 mins after I post it, I figure it out.

I was missing the INotifyProperty implementation. Thanks if anyone is looking at this.

cw
A: 

Hi

I think your ViewModel should implement the INotifyPropertyChanged interface:

    public virtual event PropertyChangedEventHandler PropertyChanged;
    protected virtual void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Then your property would look like that:

    private title;
    public string Title 
    { 
        get
        {
            return this.title;
        }

        set
        {
            if (this.title!= value)
            {
                this.title= value;
                this.RaisePropertyChanged("Title");
            }
        }
    }

Michael

Michael Ulmann
Yup that is the answer. Figured it out 10 mins after i posted.
cw