Search code examples
silverlight-2.0prismstackpanel

Databinding a StackPanel using Prism


Using Prism, 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 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


Solution

  • 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;
        }
    

    Both your xaml and my original xaml work fine.

    Thank you.

    Rick