views:

368

answers:

3

Hello,

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

A: 

The following XAML should work:

<ItemsControl x:Name="list" ItemsSource="{Binding Path=InfoBarItems}">
   <ItemsControl.ItemsPanel>
       <ItemsPanelTemplate>
           <StackPanel />
       </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
       <DataTemplate>
           <TextBox Text="{Binding Path=.}" />
       </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

The differences to your approach are:
- no StackPanel in the DataTemplate definition
- added binding path to the TextBox binding

Markus Dulghier
A: 

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

rboarman
A: 

Is you PresentationModel class implementing INotifyProperytChanged? Or is you collection a DependencyProperty? If that's not the case, the view will never get notified of the fact that you created a collection.

That's why if you set the collection BEFORE it's bound to the view it will work and not the other way around. I do believe it's bad practice not to make your PresentationModel INotifyPropertyChanged unless all the properties are fixed at binding time.

R4cOON

related questions