I want to enable selection just for specific item but i don't know how i can do this.
I try with ItemTemplateSelector but we don't have the great attribute in listviewitem.
I try with selectionChangEevent but it's doesn't work when we use SelectionMode multiple or extended.
I have a listview with SelectionMode extended and for select item i use swipe gesture with isSwipeEnable true.I want for all items with status "BAD" for example disable swipe selection but click on item for show detailview work.
Regards.
My solution =>
private void ItemList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (var item in e.AddedItems)
{
if (!item.IsBad())
{
ItemList.SelectedItems.Remove(item);
}
else
{
isBadAdd = true;
}
}
}
This code remove selectedItem in selectedItems if he isn't a bad item. But this solution don't remove the swipe enable on the item.
Related
I am a student learning xamarin forms, I am trying to create a basic chat app in this I want to know how to get position of current item in listview that's user watching. When a new message received i want to know if user is at bottom or not if at bottom focus the new and if not at the bottom then just add not by adding focus to it.
you get the selected item from the Xamarin.Forms.ListView.SelectedItem property of your ListView.
If your ListView.ItemSource is of a type that allows using IndexOf you can now do something like
int position = (yourlistview.ItemSource as ObservableCollection<your type>).IndexOf(yourlistview.SelectedItem)
Update:
ok I think i understood what you want.
In most cases more than one item is currently shown when using a listview. So their exists not a single index
but i think you just want to know if the last item of the list is visible/the user has scrolled to the end?
If so ListView has an ItemAppearing event. I use it for example to load more data from an websource if the user scrolled through the first 100 items.
You could do something like this
listview.ItemAppearing += listviewItemAppearing;
listview.ItemDisappearing += listviewItemDisappearing;
bool m_scrolledToEnd;
private void listviewItemDisappearing(object sender, ItemVisibilityEventArgs e)
{
if(e.Item == yourlastiem)
m_scrolledToEnd = false;
}
private void listviewItemAppearing(object sender, ItemVisibilityEventArgs e)
{
if(e.Item == yourlastiem)
m_scrolledToEnd = true;
}
if you realy need to know if a specific index is shown you could create a List<int> m_idxlist;
and in the appearing event add the index of the item to the list
and in the disappearing event remove the index of the item from the list.
Then you will have a list where all indexes of the items currently shown are stored.
From the Documentation
ListView supports selection of one item at a time. Selection is on by
default. When a user taps an item, two events are fired: ItemTapped
and ItemSelected. Note that tapping the same item twice will not fire
multiple ItemSelected events, but will fire multiple ItemTapped
events. Also note that ItemSelected will be called if an item is
deselected.
To detect selecting an item, you can add a method, onSelection:
void OnSelection (object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null) {
return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
}
DisplayAlert ("Item Selected", e.SelectedItem.ToString (), "Ok");
//((ListView)sender).SelectedItem = null; //uncomment line if you want to disable the visual selection state.
}
To disable selection just set the selectedItem to null:
SelectionDemoList.ItemSelected += (sender, e) => {
((ListView)sender).SelectedItem = null;
};
I have a pretty basic Datagrid XAML bound to a CollectionViewSource.
<DataGrid ItemsSource="{Binding Source={StaticResource EditingItemsCollectionViewSource}}"/>
And the Collection View Source is bound to an observable collection of very basic items with 3 numerical values. C# obviously.
I want to be able to add a new row (add a new item) at the bottom of this datagrid by pressing Tab on the keyboard when I am in the last cell of the last row.
Is this possible?
One possible solution is to programmatically set the property:
dataGrid.AllowUserToAddRows = true;
in order to implement "Add Row" functionality (provided that it was originally set to false, thus the new row was invisible). As per your task definition, it could be triggered by Tab key press (with any additional condition you may add):
private void dataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Tab)
{
e.Handled = true;
// your code
}
}
catch{}
}
You may also want to set some default values for newly created row item by adding event handling procedure:
dataGrid.InitializingNewItem += new InitializingNewItemEventHandler(dataGrid_InitNewItem);
private void dataGrid_InitNewItem(object sender, InitializingNewItemEventArgs e)
{
// your code
}
Other sample implementations of adding row to WPF DataGrid could be found here: Wpf DataGrid Add new row
Also, pertinent to your description, you can add the item to the underlying ObservableCollection, so it will automatically appear in the DataGrid.
Hope this will help. Best regards,
i have 2 listBoxes in a window, one next to the other, with buttons to copy items from one listBox to the other.
when an item from the first listBox is selected the copy button gets enabled, and remove button gets disabled. when i choose an item for the second listBox the copy button gets disabled, and remove button gets enabled.
when you select an item in one of the listBoxes the buttons change with no problem, after the listBox lost focus and you choose the same item that was selected the buttons dont change back.
i understand the problem is that the event of selected item changed will not fire, beacuse the selected item did not change.
setting the selected item to null when the listBox loses focus was not usefull beacuse i need the selcted item. i need to find a way to reselect the selected item when the listBox gains focus, or just fire the even of selected item changed. any suggestions?
You can try the ListBox.LostFocus Event and set the SelectedItem Property to null.
private void ListBox_LostFocus(object sender, RoutedEventArgs e)
{
((ListBox)sender).SelectedItem = null;
}
Use the ListBox.GotFocus event check if there is a SelectedItem, store the index, remove the SelectedItem and use the stored index to reset the SelectedItem. Something like this
private void ListBox_GotFocus(object sender, RoutedEventArgs e)
{
ListBox lb = (ListBox)sender;
if(lb.SelectedItem != null )
{
int index = lb.SelectedIndex;
lb.SelectedItem = null;
lb.SelectedIndex = index;
}
}
I have a problem with my FileOpenPicker-app. If I select an item in my GridView, the item will be added to the FileOpenPickerUI, it works fine. But if I deselect an Item in the FileOpenPickerUI, it won´t deselect in my GridView.
So I added the Event:
_fileOpenPickerUI.FileRemoved += FilePickerUI_FileRemoved;
This is the method:
private async void FilePickerUI_FileRemoved(FileOpenPickerUI sender, FileRemovedEventArgs args)
{
//DO SOMETHIG
}
This is the documentation:
http://msdn.microsoft.com/de-de/library/windows/apps/windows.storage.pickers.provider.fileopenpickerui.fileremoved
How can I react to the deselected item? How can I deselect it in my GridView?
There is no two way binding between the FileOpenPickerUI item selection/deselection and GridView item selection/deselection. If you can access the GridView's selected item within the FilePickerUI_FileRemovedhandler, you can explicitly set the SelectedIndexproperty to -1.
I have this subtle program regarding the behavior of listbox. My listbox is binded with an observable list in the viewmodel. There are 2 ways in addding an item in the listbox. First is ADD a single item then that item would be selected directly. This works fine.
The second way was LOAD which by its name will be adding more than 1 item in the lisbox. Now the problem is when loading items more than the listbox can accomodate in the view, those items that are not in view (items at the bottom thus need to be scrolled in order for it to be viewed) was not automatically selected...
Only the items that are by default viewed are the ones selected:
private void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
if (listBoxAddresses.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated) return;
for (int i = 0; i < TestSetting.DeviceSettings.Count; i++)
{
ListBoxItem myListBoxItem = (ListBoxItem)(listBoxAddresses.ItemContainerGenerator.ContainerFromItem(TestSetting.DeviceSettings[i]));
if (myListBoxItem != null)
{
myListBoxItem.IsSelected = true;
}
}
listBoxAddresses.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
}
I wonder if this is just a natural behavior for listbox.
I just realize this now...setting my listbox to :
VirtualizingStackPanel.IsVirtualizing="False"
did all the trick. Thanks to Dr.WPF for the idea. Though there are consequences for turning off virtualization (performance) but it won't matter that much.