我知道我應該使用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;
}
}
}
您需要實現NotifyPropertyChanged爲的ObservableCollection –
@MichalKozak你可以寫的代碼行,我明白了嗎?我有propertychanged方法的問題... –