I know this might sound a little basic but I'm stuck and I'm not sure how to proceed.
This is the Main Class:
public sealed partial class MainPage : Page
{
private ObservableCollection<MyClass> MyList = new ObservableCollection<MyClass>();
private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
MyList.Add(new MyClass("Hello", "World"));
MyListview.Items.Add(new MyClass("Hello", "World"));
}
private void Delete_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
MyList.Remove((MyClass)MyListview.SelectedItem);
}
}
I have a list name MyList
and a listview name MyListview
. What I want is, when I press a button, to remove the MyListview's selected item from MyList items. So something like this:
MyList.Remove((MyClass)MyListview.SelectedItem);
But for some reason this is not working for me. Nothing gets deleted this way.
The XAML of my Main Page:
<Page
x:Class="App17.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App17"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="Delete" Content="Delete" HorizontalAlignment="Left" Height="116" Margin="140,89,0,0" VerticalAlignment="Top" Width="224" Click="Delete_Click"/>
<Button Content="Button" HorizontalAlignment="Left" Height="116" Margin="442,89,0,0" VerticalAlignment="Top" Width="214" Click="Button_Click"/>
<ListView x:Name="MyListview" HorizontalAlignment="Left" Height="497" Margin="143,261,0,0" VerticalAlignment="Top" Width="510"/>
</Grid>
</Page>
And my class MyClass
is:
class MyClass
{
public string Title { get; set; }
public string Name { get; set; }
public MyClass(string title,string name)
{
Title = title;
Name = name;
}
}
I have had some trouble making sure I am really testing the code you actually want help with. But I think I have seen enough to understand what the problem you are having is.
Specifically: you don't have any connection between the ListView
where the items are displayed, and the private MyList
collection where you are storing the items.
In XAML-based apps, the whole point is to connect your data to the visual representation via bindings. One primary reason ObservableCollection<T>
exists is to facilitate this, to allow collection-based visuals to react to changes in the collection.
In your example, you should make the following changes…
Change MainPage
so that its constructor and Button_Click()
method look like this:
public MainPage()
{
this.InitializeComponent();
DataContext = MyList;
}
private ObservableCollection<MyClass> MyList = new ObservableCollection<MyClass>();
private void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
MyList.Add(new MyClass("Hello", "World"));
}
The above removes the extraneous call to MyListview.Items.Add()
(not needed when the data is bound correctly) and assigns your MyList
collection as the DataContext
for the page, making it available for binding purposes.
Then change the XAML so that the ListView
declaration looks like this:
<ListView x:Name="MyListview"
HorizontalAlignment="Left" VerticalAlignment="Top"
Height="497" Width="510"
Margin="143,261,0,0"
ItemsSource="{Binding}"/>
The key thing there is the ItemsSource
attribute. Using an empty Binding
, the control will use the current data context, which here will be inherited from the page object, which you've set in your code to be MyList
.
In this way, you need only to operate on MyList
itself, and the visual representation will update automatically.