I have a listview with data I don't know. I would like to delete a line after I press a button. The button is in the of the listview.
My code:
Xaml:
<MenuItem Clicked="SupprimerPoste_Clicked" CommandParameter="{Binding .}" />
my List:
public static ObservableCollection<Data> listePosteNoteFrais = new ObservableCollection<Data>();
App.listePosteNoteFrais.Add(new Data { Numero = Label_Numero.Text, ... });
event button:
private void SupprimerPoste_Clicked(object sender, EventArgs e)
{
var menuItem = ((MenuItem)sender);
Data delete ((Data)menuItem.CommandParameter
App.listePosteNoteFrais.Remove(delete);
}
So you can try to remove the item in the data list in the clicked event of the button. You can check the following case which shows how to delete the item in the listview.
Case:How to remove an item from a Xamarin Forms ListView?
In addition, you can check the official document about how to use the observable collection as the listview's item resource.
Official document:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/data-and-databinding
Related
I've searched and I've not found the answer yet.
I need to disable the click that happens in each item of a list and makes it be blue, as if it is selected. The problem is that my list items aren't selectables items, my list is only for the user see some informations.
you can use IsEnabled = "False" as Barney said Or otherwise asign null value to selected item after selection event.
<ListView ItemSelected="ListView_ItemSelected" />
...
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var list = (ListView)sender;
list.SelectedItem = null;
}
Add IsEnabled="False" into your ListView as shown here
<ListView IsEnabled="False">
I want to add a button to the pivot header so that it is shown in-line with pivot item titles, but doesn't update the content shown but just launches an event instead.
After some experimenting, I discovered how to do this. First place a button inside a PivotItem's header. Set it up to route tap events as well as click events.
<PivotItem>
<PivotItem.Header>
<Button
x:Name="MyButton"
Tapped="PivotItem_Tapped"
Click="MyButton_Click">MyButton</Button>
</PivotItem.Header>
</PivotItem>
Then inside the handler for the tap, consume the tap event. Clicking on the button won't update the pivot's content now:
private void MyButton_Click(object sender, RoutedEventArgs e)
{
//do whatever you need.
}
private void PivotItem_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
e.Handled = true;
}
I have defined ItemClick event handler for a ListView. The event handler is called, but when I click the ListViewItem for the first time SelectedItem of the ListView is null and SelectedIndex is -1. But I want to retrieve SelectedItem also on the first click when nothing is selected in the ListView. Is there another solution? Thanks.
As miskohut pointed out you can use ItemClickEventArgs, here is an example
private void listView_ItemClick(object sender, ItemClickEventArgs e)
{
MyItemModel item = (MyItemModel)e.ClickedItem;
//TODO:
}
The answer is an easy one.
`ItemClickEventArgs`
contains attribute ClickedItem which is type of type DataContext of ListView.
More appropriately for the click event, get the clicked item index. For example,
private void Column_ItemClick(object sender, ItemClickEventArgs e)
{
ListView listView = (ListView)sender;
Tile clickedMenuItem = (Tile)e.ClickedItem; // Typecast the clicked menu item back to the content type of each list view item
// Key line is here:
int itemNumber = listView.Items.IndexOf(clickedMenuItem);
}
In the above on click event of the listView.ItemClick += Column_ItemClick event, the itemNumber integer is the index of the clicked list view item.
This solution avoids the -1 index response for a selected item, because the on click event that selects an item happens only after the second click of the menu.
I have a listView which is set to Mode=TwoWay, which i thought was suppose to refresh the views, if the underlying data changed.
However, while the item is deleted correctly, the item still remains on the list, until I exit and return to the page.
Xaml:
<ListView ItemsSource="{Binding Path=items, Mode=TwoWay}" >
<DataTemplate>
...
<Button x:Name="btn_delete_item" Click="btn_delete_item_Click" >
</Button>
Behind code:
private void btn_delete_item_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
itemType item = button.DataContext as itemType;
items.Remove(item);
}
In order to fully support data binding, your Items collection must notify about changes, i.e. whether items are added, removed, replaced or moved. This notification is done by implementing the INotifyCollectionChanged interface. The framework's List<T> type does not implement this interface, but ObservableCollection<T> does.
So you could simply change the type of your Items property:
public ObservableCollection<ItemType> Items { get; set; }
I want to prevent the ability to deselect a list view item if it is already selected. Therefore how do I prevent right mouse click ability to deselect an item?
I have prevented the ability to deselect via swiping by using IsSwipeEnabled="False" on the List View. I didn't require swipe ability on the list view.
I'm happy to completely prevent right mouse click on the list view items if needed.
If I am reading your question correctly, it sounds like you want the ability for the user to select items, but to not be able to de-select. If that is the case, it seems like a strange requirement - it goes against normal UI convention and does something that the user is not expecting.
Having said that, you can do so by handling the SelectionChanged event in the ListView.
When the event is triggered, it gives you a list of removed (de-selected) items. You then just need to add those items back into the ListView's selected items list:
private void itemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (var item in e.RemovedItems)
{
itemListView.SelectedItems.Add(item);
}
}
Note that if you use the above code, you do not have to handle any swipe or mouse events.
Edit - Per OP's comment, the requirement is slightly different than what I thought:
I want the selected item to deselect if a different item is selected. however what I dont want is an already selected item to be (manually) deselected
Assuming that you have a single select ListView, you can still use the SelectionChanged event and the SelectionChangedEventArgs to do what you are asking for:
private void itemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count > 0 && e.AddedItems.Count == 0)
{
var removed = e.RemovedItems[0];
itemListView.SelectedItem = removed;
}
}
I have found a simple solution on the following forum.
You simply add a RightTapped event to the ListView DataTemplate content.
<ListView>
<ListView.ItemTemplate>
<DataTemplate>
<ContentPresenter RightTapped="daves_RightTapped" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And then in the code behind:
private void daves_RightTapped(object sender, Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e)
{
e.Handled = true;
}
This works fine on an outlook style ListView.