2009-07-04 88 views
0

使用棱鏡,我實現了一個View,Model和Presenter,就像StockTraderRI項目一樣。我的問題是,我試圖將一個堆棧面板綁定到ObservableCollection對象,但沒有顯示任何字符串。使用棱鏡數據綁定StackPanel

這裏是我的代碼:

PresentationModel代碼:

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代碼:

<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> 

我試圖綁定的多種組合,但還沒有弄清楚,爲什麼我的琴絃從不顯示向上。我究竟做錯了什麼?

裏克

回答

0

事實證明,如果我在分配模型之前創建我的集合,它就會起作用。

原始代碼:

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 InfoBarPresentationModel(IInfoBarView view, IEventAggregator eventAggregator) 
    { 
     InfoBarItems = new ObservableCollection<string>(); 
     InfoBarItems.Add("Test 1"); 
     InfoBarItems.Add("Test 2"); 
     this.View = view; 
     this.View.Model = this; 
    } 

無論你的XAML和我原來的XAML做工精細。

謝謝。

裏克

0

下面的XAML應該工作:

<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> 

你的方法的區別在於:
- 沒有的StackPanel中的DataTemplate定義
- 新增結合路徑文本框結合

0

是你實現INotifyProperytChanged PresentationModel類?或者你收集DependencyProperty?如果情況並非如此,視圖將永遠不會收到您創建集合的事實的通知。

這就是爲什麼如果你設置集合之前它是綁定到視圖它將工作,而不是相反。我相信這是不好的做法,不要讓你的PresentationModel INotifyPropertyChanged,除非所有的屬性在綁定時間固定。