How to get current visible group name in CollectionView when scrolling?
I want to show this info in page title.
myCollectionView.Scrolled += (object sender, ItemsViewScrolledEventArgs e) =>
{
int first_index = e.FirstVisibleItemIndex;
};
You could get the index and then get the matched item to get the group name.
int first_index = e.FirstVisibleItemIndex;
if (first_index!=0)
{
Name = ObservCollectionCategory[first_index-1].Category_Name;
//The ObservCollectionCategory is the item source of Collectionview.
}
And then binding the name of the title.
Title="{Binding Name}"
Related
in my app I am loading phone numbers from database to listview, this is done and work fine. Now I want when user click at any number in listview the app must dialing the selected number. How to get the list item value and make call directly when clicked.
String[] myArr = (String[])records.ToArray(typeof(string));
//setting array adapter with item layout
ArrayAdapter adapter = new ArrayAdapter(this, Resource.Layout.item, Resource.Id.itemTxt, (System.Collections.IList)records);
list.SetAdapter(adapter); // setting adapter to list view
list.ItemClick += List_ItemClick;
}
void List_ItemClick(object sender, AdapterView.ItemClickEventArgs e )
{
var uri = Android.Net.Uri.Parse("tel:" +???? );
var intent = new Intent(Intent.ActionDial, uri);
StartActivity(intent);
}
I'm using MultiselectList from Controls.Toolkit. I use it as a favourite selector. I have a list with items, I select the favourites and the next time I open the selection bar I would like to see my favourites already selected. When IsSelectionEnabledChanged event occurs, if IsSelectionEnabled is true (the selection bar is opened) I try to add my favourites to the list's SelectedItems. Here is a code snippet:
private void multiSelectList_IsSelectionEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (multiSelectList.IsSelectionEnabled)
{
foreach (var favourite in FavouritesList)
{
multiSelectList.SelectedItems.Add(multiSelectList.Items.Where(i => ((MyModel)i).id == favourite.id).FirstOrDefault());
}
}
}
I have tested this solution and I found out that the layout does not update the entire list that's why I'm not seeing the items as selected (but they are). Not even the actual visible items in the list. After scrolling for a bit and scrolling back, the selection appears! I've tried to use multiSelectList.UpdateLayout() method programatically but it did not solve it.
I wonder if it is a visualization problem or a CheckBox binding problem (the selection uses CheckBox on the side).
The SelectedItems is just a List<object>, it doesn’t raise any events when you update it.
To update your items manually, you could do something like following instead (untested code):
private void multiSelectList_IsSelectionEnabledChanged( object sender, DependencyPropertyChangedEventArgs e )
{
if( !multiSelectList.IsSelectionEnabled )
return;
var dictSelected = FavouritesList.ToDictionary( f => f.id, f => true );
for( int i = 0; i < multiSelectList.Items.Count; i++ )
{
MyModel m = (MyModel)multiSelectList.Items[ i ];
if( !dictSelected.ContainsKey( m.id ) )
continue; // Not selected
MultiselectItem item = (MultiselectItem)multiSelectList.ItemContainerGenerator.ContainerFromIndex( i );
if( null != item )
item.IsSelected = true; // This should add the item into the SelectedItems collection.
else
multiSelectList.SelectedItems.Add( m ); // The item is virtualized and has no visual representation yet.
}
}
I have a ListBox, here i am binding IList Like (Cities).
I want an event like OnItemDataBound in .NET in Windows Phone7.
Like when every city was binded (if 10 cities are this event will fire 10 times) this event will fire so that i have to do some more calculations on this event. In this event i have to find the object binded to the ListItem so that i can make some calculations. Is there any event in WP7 similar OnItemDataBound in .NET.
<ListBox Loaded="lstFlights_Loaded" Height="635" x:Name="lstFlights"
ScrollViewer.VerticalScrollBarVisibility="Auto" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<Border CornerRadius="10" Margin="10">
<Border.Background>
<ImageBrush ImageSource="../Images/rowbg.png"></ImageBrush>
</Border.Background>
//Some data here
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
i am binding the data like below:
lstFlights.ItemsSource = objCities;
on Binding every city i want an event to populate some list items(like: i want to change the textblock text,etc) according to the City that binded to ListItem. To do this I need an event like OnItemDataBound in WP7. And I have list picker like below:
On SelectionChanged event also i want to change the list items.
And one more thing IList(objCities) is coming from the Service so I can't change that Object
so if i want to change any TextBlock in List Box i have do FindName and i have to assign calculated value for every city binded.
There is an CollectionChanged event in ObservableCollection collection for new elements added/removed.
ListBox provides some methods for accessing ListBoxItem. You can see them in list.ItemContainerGenerator class.
You can use it together to achieve needed results.
There is short example that shows how to make a red foreground to newly added items in some conditions (even or not):
ObservableCollection<string> items = new ObservableCollection<string>();
public MainPage()
{
InitializeComponent();
items.CollectionChanged += (s, e) =>
{
Dispatcher.BeginInvoke(() =>
{
if (e.NewItems != null)
{
foreach (var item in e.NewItems)
{
ListBoxItem listitem = (ListBoxItem)list.ItemContainerGenerator.ContainerFromItem(item);
if (DateTime.Parse(listitem.Content.ToString()).Second % 2 == 0)
{
listitem.Foreground = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
}
}
}
});
};
list.ItemsSource = items;
}
private void AddButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
items.Add(DateTime.Now.ToLongTimeString());
}
private void RemoveButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
Random rnd = new Random();
if (items.Count > 0)
{
int index = rnd.Next(0, items.Count);
items.RemoveAt(index);
}
}
}
How about using the ItemsChanged event of your ListBox's ItemContainerGenerator instead?
this.myListBox.ItemContainerGenerator.ItemsChanged += new System.Windows.Controls.Primitives.ItemsChangedEventHandler(ItemContainerGenerator_ItemsChanged);
void ItemContainerGenerator_ItemsChanged(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
...
If hold the listbox, I want to get listbox index.
This is my code:
<ListBox Margin="0,0,-12,0"
Hold="holdlistbox"
x:Name="listbox"
SelectionChanged="listbox_SelectionChanged"
SelectedIndex="-1">
</ListBox>
private void holdlistbox(object sender, System.Windows.Input.GestureEventArgs e)
{
//How to get ListBox index here
}
If anyone knows help me to do this.
e.OriginalSource will get you the actual control that was held (the top-most control directly under your finger). Depending on your ItemTemplate and where you hold then this could be any of the controls in the item. You can then check the DataContext of this control to get the object that is bound to that item (going by your comment this will be an ItemViewModel object):
FrameworkElement element = (FrameworkElement)e.OriginalSource;
ItemViewModel item = (ItemViewModel)element.DataContext;
You can then get the index of this item in the items collection:
int index = _items.IndexOf(item);
If you want to get the ListBoxItem itself you will need to use the VisualHelper class to search the parent heirarchy. Here is an enxtension method that I use to do this:
public static T FindVisualParent<T>(this DependencyObject obj) where T : DependencyObject
{
DependencyObject parent = VisualTreeHelper.GetParent(obj);
while (parent != null)
{
T t = parent as T;
if (t != null)
{
return t;
}
parent = VisualTreeHelper.GetParent(parent);
}
return null;
}
I'm not sure if you need this (I couldn't be sure from your comment) but you can then do the following to get the context menu:
FrameworkElement element = (FrameworkElement)e.OriginalSource;
ListBoxItem listItem = element.FindVisualParent<ListBoxItem>();
ContextMenu contextMenu = ContextMenuService.GetContextMenu(listItem);
This assumes that the ContextMenu is attached to the ListBoxItem, if not then you need to search for a different control in the parent heirarchy.
var selectedIndex = (sender as ListBox).SelectedIndex;
Is there a way to get the text of a clicked listbox item?
So on click it sets a string to the text in the list box item.
In a new DataBound App, changed the following method to see 3 ways of getting this:
// Handle selection changed on ListBox
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// If selected index is -1 (no selection) do nothing
if (MainListBox.SelectedIndex == -1)
return;
var string1 = ((sender as ListBox).SelectedItem as ItemViewModel).LineOne;
var string2 = (MainListBox.SelectedItem as ItemViewModel).LineOne;
var string3 = (e.AddedItems[0] as ItemViewModel).LineOne;
// Navigate to the new page
NavigationService.Navigate(new Uri("/DetailsPage.xaml?selectedItem=" + MainListBox.SelectedIndex, UriKind.Relative));
// Reset selected index to -1 (no selection)
MainListBox.SelectedIndex = -1;
}