The issue is already explained in the title. Please take a close look at the XAML and code because I'm a amateur in C# (only basic knowledge) and almost completely new to Data Binding. So here is my XAML:
<ListBox x:Name="BoardList" ItemsSource="{Binding notes, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" >
<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>
Than I create an ObservableCollection
in the Mainpage.xaml.cs
file:
public sealed partial class MainPage : Page
{
ObservableCollection<BoardNote> notes = new ObservableCollection<BoardNote>();
public MainPage() //empty for post
{
this.InitializeComponent();
}}
The class BoardNote
:
class BoardNote : NotificationObject
{
private string _text { get; set; }
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;
}
}
}
And the Notification
class:
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;
}
If I create a new Boardnote
eg over a button the Listbox
doesn't add a new item. I don't know what to do and I'm new to binding, and to the INotifyPropertyChanged
class but I understand new things quickyly.
1.- DataContext is an instance of a class that contains the data you need. You can set the datacontext like:
<Page xmlns:local...>
<Page.DataContext>
<local:ViewModelClass/>
</Page.DataContext>
(You can do in several ways this, as a resource and many other things)
2.- The class ViewModelClass should contain a property called Notes and must be a property.
public ObservableCollection<BoardNote> Notes {get;} = new ObservableCollection<BoardNote>();
3.- If you need to update the data do not reinstance Notes, just clear it and readd items.
4.- It is interesting to implement INotifyPropertyChanged to make the UI refresh the View with the new data.
UPDATE:
If you set the datacontext to an Instance of Notes:
<ListBox x:Name="BoardList" ItemsSource="{Binding}">
<ListBox.DataContext>
<local:Notes/>
</ListBox.DataContext/>
...
But as you can see it is a bit difficult because Notes should be a class so you can inherit from ObservableCollection:
public class Notes : ObservableCollection<Note>
{
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.