绑定到列表框 ui 的 ObservableCollection 不更新
ObservableCollection binding to listbox ui doesn't update
我知道我应该使用 MVVM 模式,但我正在努力逐步接近它。所以这是我的列表框:
<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的按钮,点击事件是这样的:
notes.Add(new BoardNote(UserInputNote.Text));
所以现在 UI 应该在用户每次单击按钮保存新笔记时更新。但是没有任何反应。我做错了什么?
如果您需要,这里是 BoardNote class:
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;
}
}
}
您需要实施 INotifyPropertyChanged。这是一种方法。
创建此 NotificationObject class。
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;
}
那么你的 BoardNote class 将以这种方式继承它:
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;
}
}
}
我知道我应该使用 MVVM 模式,但我正在努力逐步接近它。所以这是我的列表框:
<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的按钮,点击事件是这样的:
notes.Add(new BoardNote(UserInputNote.Text));
所以现在 UI 应该在用户每次单击按钮保存新笔记时更新。但是没有任何反应。我做错了什么?
如果您需要,这里是 BoardNote class:
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;
}
}
}
您需要实施 INotifyPropertyChanged。这是一种方法。
创建此 NotificationObject class。
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;
}
那么你的 BoardNote class 将以这种方式继承它:
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;
}
}
}