2015-08-21 52 views
0

我知道我應該使用MVVM模式,但我試圖逐步接近它。因此,這裏是我的列表框:ObservableCollection綁定到列表框ui不更新

<ListBox x:Name="BoardList" ItemsSource="{Binding notes}" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
         <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> 
          <TextBox IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Visible" Text="{Binding text}" TextWrapping="Wrap" Foreground="DarkBlue"></TextBox> 
          <AppBarButton Visibility="{Binding visibility}" Icon="Globe" Click="OpenInBrowser" x:Name="Link"></AppBarButton> 
          <AppBarButton Icon="Copy" Click="Copy"></AppBarButton> 
          <AppBarButton Icon="Delete" Click="Delete"></AppBarButton> 
         </StackPanel> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

在MainPage.xaml.cs我聲明如下:

ObservableCollection<BoardNote> notes = new ObservableCollection<BoardNote>(); 

所以,如果我理解這一點對的,我不需要關心「INotifyCollectionChanged」因爲我正在使用observablecollection? 所以我就比如像這樣的文本框:

<Textbox x:Name="UserInputNote" Placeholdertext="Type in a text for your note"></Textbox> 

和一個按鈕,新添加備註的ObservableCollection和click事件就是這樣:

notes.Add(new BoardNote(UserInputNote.Text)); 

所以,現在出現的用戶界面每次用戶點擊按鈕以保存新筆記時更新。但沒有任何反應。我做錯了什麼?

如果你需要在這裏它是板注類:

class BoardNote 
{ 
    public string text 
    { 
     get; set; 
    } 
     public BoardNote(string text) 
    { 
     this.text = text; 
    } 
    public Visibility visibility 
    { 
     get 
     { 
      if (text.StartsWith("http")) 
       return Visibility.Visible; 
      else 
       return Visibility.Collapsed; 
     } 
    } 
} 
+0

您需要實現NotifyPropertyChanged爲的ObservableCollection –

+0

@MichalKozak你可以寫的代碼行,我明白了嗎?我有propertychanged方法的問題... –

回答

0

您需要執行INotifyPropertyChanged。這是一種做法。

創建此NotificationObject類。

public class NotificationObject : INotifyPropertyChanged 
{ 
    protected void RaisePropertyChanged<T>(Expression<Func<T>> action) 
    { 
     var propertyName = GetPropertyName(action); 
     RaisePropertyChanged(propertyName); 
    } 

    private static string GetPropertyName<T>(Expression<Func<T>> action) 
    { 
     var expression = (MemberExpression)action.Body; 
     var propertyName = expression.Member.Name; 
     return propertyName; 
    } 

    private void RaisePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

然後你的板注類將繼承這種方式:

class BoardNote : NotificationObject 
{ 
private string _text 
public string Text 
{ 
    get {return _text;} 
    set 
    { 
    if(_text == value) return; 
    _text = value; 
    RaisePropertyChanged(() => Text); 
    } 
} 
    public BoardNote(string text) 
{ 
    this.text = text; 
} 
public Visibility visibility 
{ 
    get 
    { 
     if (text.StartsWith("http")) 
      return Visibility.Visible; 
     else 
      return Visibility.Collapsed; 
    } 
} 
} 
+0

謝謝,幫助我 –

相關問題