Binding to a StackPanel

Topics: Prism v2 - Silverlight 2
Jul 4, 2009 at 10:07 PM

Hello,

I have implemented a View, Model and Presenter much like the StockTraderRI project. My issue is that I am trying to databind a stackpanel to an ObservableCollection<string> object but no strings are being displayed.

Here’s my code:

PresentationModel code:

        public InfoBarPresentationModel(IInfoBarView view, IEventAggregator eventAggregator)

        {

            this.View = view;

            this.View.Model = this;

            InfoBarItems = new ObservableCollection<string>();

            InfoBarItems.Add("Test 1");

            InfoBarItems.Add("Test 2");

        }

 

        public IInfoBarView View { get; set; }

 

        public ObservableCollection<string> InfoBarItems { get; set; }

XAML code:

    <ItemsControl x:Name="list" ItemsSource="{Binding InfoBarItems}">

        <ItemsControl.ItemsPanel>

            <ItemsPanelTemplate>

                <StackPanel />

            </ItemsPanelTemplate>

        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>

            <DataTemplate>

                <StackPanel Orientation="Horizontal">

                    <TextBox Text="{Binding}"/>

                </StackPanel>

            </DataTemplate>

        </ItemsControl.ItemTemplate>

    </ItemsControl>

I have tried numerous combinations of bindings but have yet to figure out why my strings never show up. What am I doing wrong?

Rick

 

Jul 6, 2009 at 1:43 AM

Have you made sure that you're setting the View's DataContext properly?  The source looks proper and I've used the same syntax and it worked.

Jul 6, 2009 at 3:02 AM

In my view's code behind I have:

public IInfoBarPresentationModel Model

{

        get { return this.DataContext as IInfoBarPresentationModel; }

      set { this.DataContext = value; }

}

In my PresentationModel , as shown above, I have this:

this.View.Model = this;

The above setter is being hit when I run the program.

Any ideas on how I can debug the view?

Thanks,

Rick

 

<font face="Courier New" size="2">

 

</font>

 

 

Jul 6, 2009 at 3:23 AM

One possible problem may be that you might be setting the DataContext AFTER the control binding is being constructed???  Anyway.... a very helpful tool for debugging binding is the following method (from Bea Stollnitz's blog):

<Window …
    xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    />
    
    <TextBlock Text="{Binding Path=Caption, diagnostics:PresentationTraceSources.TraceLevel=High}" … />

It causes a nice detail of the binding processing to be sent to the Output debug window.

Good luck...

Jul 6, 2009 at 6:58 PM

It turns out that if I create my collection before assigning the Model, it works.

Original code:

      public InfoBarPresentationModel(IInfoBarView view, IEventAggregator eventAggregator)
        {
            this.View = view;
            this.View.Model = this;
            InfoBarItems = new ObservableCollection<string>();
            InfoBarItems.Add("Test 1");
            InfoBarItems.Add("Test 2");
        }

New code:

      public InfoBarPresentationModel(IInfoBarView view, IEventAggregator eventAggregator)
        {
            InfoBarItems = new ObservableCollection<string>();
            InfoBarItems.Add("Test 1");
            InfoBarItems.Add("Test 2");
            this.View = view;
            this.View.Model = this;
        }

The original xaml code works fine.

Thank you.

Rick

Jul 6, 2009 at 7:18 PM

OK... Sorry I didn't see this before.  Your problem is that your PresentationModel needs to do one of two things to be a proper binding source in MVVM pattern:

a) Implement INotifyPropertyChanged and raise the PropertyChanged event for all bindable properties

OR

b) Make InfoBarItems a DependencyProperty (you may have to make your PresentationModel class inherit from DependencyObject)

If you want to have the view update when the underlying data changes, you'll need to implement one of these two proper fixes.