Silverlight MVVM Problem…Question

Well, I’m struggling with a few items with Silverlight, MVVM and databinding.

After reading this good article here on msdn http://msdn.microsoft.com/en-us/magazine/dd458800.aspx

I ran through the sample, looks pretty good, etc…

Few problems though…

1. the ViewModel constructor.  If I use this, I can’t use Blend any longer.  I get a memory corruption message.  I tried it on 2 different projects, same issue.  Not sure what is behind it

public GamesViewModel() :
      this(new GameCatalog())
    {
    }

    public GamesViewModel(IGameCatalog catalog)
    {
      theCatalog = catalog;
      theCatalog.GameLoadingComplete +=
        new EventHandler<GameLoadingEventArgs>(games_GameLoadingComplete);
      theCatalog.GameLoadingError +=
        new EventHandler<GameCatalogErrorEventArgs>(games_GameLoadingError);
    }

2. In my sample, similiar to this one in the article… my ‘Games’ returned are in the thousands – ie. 3000 records.   I only want to show the top 50.   I’ve been unable to get this work and resorted to manually coding up the binding in the code behind of the view – something of course I’m trying to avoid with this model.

ie. here is my viewModel:

 

public ServicesView()
        {
            InitializeComponent();
            Loaded += OnPageLoaded;

            viewModel = Resources["ServicesViewModel"] as ServicesViewModel;
            if (viewModel != null)
            {
                viewModel.LoadComplete += viewModelLoadComplete;
                viewModel.GetServices();
            }
        }

void viewModelLoadComplete(object sender, EventArgs e)
        {
            FilterCriteria.ItemsSource = viewModel.FilteredServiceList;
            ServiceListBox.ItemsSource = viewModel.FilterByCriteria(string.Empty);
            ProgressIndicator.IsActive = false;
            ServiceContent.Visibility = Visibility.Visible;
        }

The two bolded lines…I want to bind using the XAML, but it’s not taking.  Seems to me it’s related to the order in which things are occuring.

ie. here is the main list coming back from the database:

public ObservableCollection<ServiceClient> ServiceClients
        {
            get
            {
                return serviceClients;
            }
        }

That list receives 3000 service clients on load complete.

The ServiceListBox is bound in xaml, this will work, but gets 3000+

<ListBox x:Name="ServiceListBox" Height="320" Width="448" HorizontalAlignment="Left" VerticalAlignment="Bottom" 
                     Grid.Row="1"
                     Grid.ColumnSpan="2"
                     Style="{StaticResource ListBoxStyle}"
                     ItemsSource="{Binding ServiceClients, Source={StaticResource ServicesViewModel}}"
                     ItemTemplate="{StaticResource ServiceClientsTemplate}" Margin="32,0,0,36" d:UseSampleData="True"/>

So, what I tried to do was filter it out:

ViewModel:

ie.

public List<ServiceClient> FilteredServiceClient
        {
            get
            {
                return ServiceClients.OrderBy(c => c.ServiceDescription).Take(50).ToList();
            }
        }

<ListBox x:Name="ServiceListBox" Height="320" Width="448" HorizontalAlignment="Left" VerticalAlignment="Bottom" 
                     Grid.Row="1"
                     Grid.ColumnSpan="2"
                     Style="{StaticResource ListBoxStyle}"
                     ItemsSource="{Binding FilteredServiceClient, Source={StaticResource ServicesViewModel}}"
                     ItemTemplate="{StaticResource ServiceClientsTemplate}" Margin="32,0,0,36" d:UseSampleData="True"/>

However, this data is never bound to the list… (and I’ve tried making this list an observablecollection, same problem)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s