Xamarin - How to know what has been clicked in collection view? - xamarin

I have a CollectionView with an image and a button in it. I use following code to see if somebody pressed anywhere within the cell:
private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (((CollectionView)sender).SelectedItem != null)
{
var item = (picdata)e.CurrentSelection.FirstOrDefault();
((CollectionView)sender).SelectedItem = null;
if (allowfullscreen == "1" || allowfullscreen == "true")
{
Navigation.PushAsync(new Picture());
}
}
}
But how can I know if he clicked the button inside the cell? I was trying to do it via the Click event, but then I do not know which one of all the buttons has been clicked.

you can get the item from the BindingContext of the sender
var item = (picdata)(Button)sender.BindingContext;

Related

Xamarin.Android: Click event on Textview inside list view cell called multiple times

I have a list view in which inside each cell I have two textview, I need to handle the click event for one of the textview element. But when I put the click event inside the GetView() of the adapter, it is called multiple times.
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = tableItems[position];
ViewHolder holder;
View view = convertView;
if (view == null)
{
LayoutInflater layoutInflator = LayoutInflater.From(mContext);
view = layoutInflator.Inflate(Resource.Layout.myListViewCell, null);
holder = new ViewHolder();
holder.tvEmpName = view.FindViewById<TextView>(Resource.Id.tv_EmpName);
holder.tvEmpPhone = view.FindViewById<TextView>(Resource.Id.tv_EmpPhone);
view.Tag = holder;
}
else
{
holder = (ViewHolder)view.Tag;
}
holder.tvEmpName.Text = item.FullName;
holder.tvEmpPhone.Text = item.Phone;
holder.tvEmpPhone.Click += (sender, e) => {
// Click event to launch the Popup menu
// This event is being called multiple times, as Get view() being called multiple times.
};
return view;
}
I gone through this similar thread, but didn't find any solution.
The event is called multiple times because evertime when the metod getview is called you add a click event with +=.
In this case you can put the click event inside the if, like that:
if (view == null)
{
LayoutInflater layoutInflator = LayoutInflater.From(mContext);
view = layoutInflator.Inflate(Resource.Layout.myListViewCell, null);
holder = new ViewHolder();
holder.tvEmpName = view.FindViewById<TextView>(Resource.Id.tv_EmpName);
holder.tvEmpPhone = view.FindViewById<TextView>(Resource.Id.tv_EmpPhone);
view.Tag = holder;
holder.tvEmpPhone.Click += (sender, e) => {
// Code here
};
}
But the recyclerview is better than listview for this.

ListBox and selectedIndexChanged event after the user hit the back button

In my windows phone 7 app, I have the following code to handle the OnSelectedIndexChange of a ListBox.
private void wordList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
WordList selectedList = (WordList)e.AddedItems[0];
NavigationService.Navigate(new Uri("/Views/Game.xaml?ListName=" + selectedList.Name, UriKind.RelativeOrAbsolute));
}
The above code work fine, However if the user click on the hardware back button from the Game page, and click on the same listbox item, the above code is not called. I assume this is because the selected item is the same therefore the SelectionChanged event is not being called.
How do I make it so that if the user select the same item I can still send them to the Game page?
I looked at the Tap Event but I couldn't figure out a way to get the selected Item from the tab event.
When using SelectionChanged to navigate, surround your navigation logic with a check to see if the SelectedIndex = -1. After you navigate, set the index to -1, so that the event will not fire twice.
private void wordList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var lb = sender as ListBox;
if (lb != null)
{
if (lb.SelectedIndex == -1) return;
WordList selectedList = (WordList)e.AddedItems[0];
NavigationService.Navigate(new Uri("/Views/Game.xaml?ListName=" + selectedList.Name, UriKind.RelativeOrAbsolute));
lb.SelectedIndex = -1;
}
}
This way you can get the selected Item from the Tap event.
private void wordList_Tap(object sender, GestureEventArgs e)
{
var selectedElement = e.OriginalSource as FrameworkElement;
if (selectedElement != null)
{
var selectedData = selectedElement.DataContext as WordList;
if (selectedData != null)
{
NavigationService.Navigate(new Uri("/Views/Game.xaml?ListName=" + selectedData.Name, UriKind.RelativeOrAbsolute));
}
}
}
I had such issue within a UserControl. Check the sender, and return if it is not the ListBox control which is triggering the event:
protected void cbEvents_SelectedIndexChanged(object sender, EventArgs e)
{
if (sender is DropDownList)
RebindGrid();
}

Rad grid localization

How do i localize the text that is written in the bottom when pagination is done in the radgrid. For Example text which displays the page number Page No. 1 2 3
I'm not sure in which context you use the RagGrid, but there is how I changed the pager strings for RagGrid in my ASP.NET project:
protected override void OnItemDataBound(GridItemEventArgs e)
{
// Localize pager.
if (e.Item is GridPagerItem)
{
var pager = (GridPagerItem)e.Item;
var label = pager.FindControl("GoToPageLabel") as Label;
if (label != null)
label.Text = "Page #:";
label = pager.FindControl("PageOfLabel") as Label;
if (label != null)
label.Text = string.Format(", total: {0}", label.Text.Substring(label.Text.IndexOf(' ')));
label = pager.FindControl("ChangePageSizeLabel") as Label;
if (label != null)
label.Text = "Items per page:";
var button = pager.FindControl("GoToPageLinkButton") as Button;
if (button != null)
button.Text = "Go to this page";
button = pager.FindControl("ChangePageSizeLinkButton") as Button;
if (button != null)
button.Text = "Change page size";
return;
}
base.OnItemDataBound(e);
}
I assuming that there is almost same way to do it for WinForms. Also you can subscribe to ItemDataBound event instead of overriding the protected method.
There are RadControls for various presentation frameworks, you should be more specific.
If you're using AJAX ASP.NET, it should be something along PagerTooltipFormatString in RadGrid.PagerStyle (see here).

Selecting First Item in the ListBox on CollectionViewSource.Filter

My ListBox is Binded with CollectionView Source. When I am Changing the Filter it is Automatically selecting the First Item in the Listox.
App.ViewModel.TasksViewSource.Filter += new System.Windows.Data.FilterEventHandler(Tasks_Filter);
void Tasks_Filter(object sender, System.Windows.Data.FilterEventArgs e)
{
if (e.Item == null)
return;
Task task = e.Item as Task;
e.Accepted = task.Id.Equals(TaskId);
}
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (TasksListBox.SelectedIndex == -1)
return;
Task selectedTask = App.ViewModel.AllTasks[TasksListBox.SelectedIndex];
TasksListBox.SelectedIndex = -1;
NavigationService.Navigate(new Uri("/Views/TaskDetailsPage.xaml?taskId=" + selectedTask.Id, UriKind.Relative));
}
Please Help Me.
Set your ListBox IsSynchronizedWithCurrentItem="False".
What really do you want ?
I you do not want the first item being selected when changing filter, you have first to create a private Task object (and/or a SelectedTask property implementing INotifyPropertyChanged).
On the SelectionChanged event of your listbox, set the SelectedTask with the current selected Task.
Then, after having applied your filter, bind the SelectedItem property to SelectedTask.

Silverlight TabItem template not working correctly

In a SL4 application i need to restyle my TabItems (actually add a button in the header).
So i took the TabItem's control template from here and added the functionality i wanted.
This seems to work fine, (i could dynamically add tabitems) with one exception:
i think this posted control template is behaving somehow "arbitrary": every time the mouse hoovers over a non selected TabItem header, this gets selected WHITHOUT clicking!! (afaik this is not the default behavior: the user user has to click a header to make this tabitem the selected one).
I tried to find why it is behaving like this, with no luck!
Is there someone who can enlighten my darkness???
Thanks in advance!
Well it turns out the error was not in the control template but in the class, the style was applied to.
In detail: the class the style was applied to is the following (in it you will see my comment about the "wrong behavior"):
public class WorkspaceViewModel : TabItem
{
public WorkspaceViewModel()
{
DefaultStyleKey = typeof(WorkspaceViewModel);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Button closeButtonSel = base.GetTemplateChild("PART_CloseTopSelected") as Button;
Button closeButtonUnsel = base.GetTemplateChild("PART_CloseTopUnSelected") as Button;
if (closeButtonSel != null)
closeButtonSel.Click += new RoutedEventHandler(closeButtonSel_Click);
if (closeButtonUnsel != null)
closeButtonUnsel.Click += new RoutedEventHandler(closeButtonSel_Click);
//this part is causing the effect i was complaining about!
//and has to be removed
this.MouseEnter += delegate(object sender, MouseEventArgs e)
{
IsSelected = true;
};
}
void closeButtonSel_Click(object sender, RoutedEventArgs e)
{
//this is the close request method used in the CloseTabItemCommand
OnRequestClose();
}
#region CloseTabItemCommand
private RelayCommand closeTabItemCommand;
public ICommand CloseTabItemCommand
{
get
{
if (this.closeTabItemCommand == null)
this.closeTabItemCommand = new RelayCommand(p => this.OnRequestClose(), p => this.CanCloseTabItem());
return this.closeTabItemCommand;
}
}
private bool CanCloseTabItem()
{
return true;
}
public event EventHandler RequestClose;
private void OnRequestClose()
{
if (RequestClose != null)
RequestClose(this, EventArgs.Empty);
}
#endregion
}

Resources