I've read somewhere that longlistselector from the toolkit is better in performance than the existing listbox. So, I changed the listbox to longlistselector. Now I have a image button control to keep in the longlistselector (that acts like a checkbox). When I click on the button, the list selection changed event is fired along with the button click. The button in listbox works fine as expected but not in longlistselector. How can I stop the list selection changed event? I searched a lot on this but couldn't find anything useful. First of all is it possible?
I wouldn't take it for granted that the long list selector performs better than the listbox. The listbox uses a virtualizing stack panel when binding is involved and is pretty performant. I went down the road of using the list picker from the toolkit and ended up regretting it due to some bad performance problems. If it works with the listbox I'd say stick with the listbox and only move away if you find you have perfomance issues in the future.
When a button is clicked a button event handler is triggered and when a item in the long list selector is changed the corresponding selection changed event is triggered IF it is also registered. But the button is clicked on the same selected item, only the button event handler is triggered. I suggest to have only a button event handler and get the selected item from it.
private void ButtonEvent_Click(object sender, RoutedEventArgs e)
{
HoldingClass clicked=((sender as Button).DataContext as HoldingClass);
//Do something with the HoldingClass as this is the binding element to the long list selector
}
Change the ClickMode to Press in XAML
ClickMode="Press"
and inside you Click event handler make (YourListName).SelectedItem = null;
private void deleteButton_Click(object sender, RoutedEventArgs e)
{
MainLongListSelector.SelectedItem = null;
..
}
Related
I have a custom DatePicker, which is opened if the user taps on it. It uses the same mechanics like the Xamarin.Forms DatePicker. The scenario is like the following:
user taps on the custom DatePicker
instead of picking a date he opens another dialog
two dialogs are now opened at the same time
I tried to Unfocus() if the other element get's the focus, but nothing happens. The DatePicker is still displayed.
What else can I do? Should I throw manually the Unfocus event? Can I lock the UI somehow so that the user has to press the Finish button before moving on?
First, the issue doesn't occur if I switch between DatePicker and TimePicker. It only occurs if I switch from DatePicker/TimePicker to my Entry (which triggers a custom dialog in Focused event).
And Unfocus() does work, if you do it on the UI thread:
private async void someEntry_Focused(object sender, FocusEventArgs e)
{
Device.BeginInvokeOnMainThread(() =>
{
this.datePicker.Unfocus();
});
// custom dialog shown to user ...
}
I am implementing an autocomplete view in my mobile app that i am developing using Xamarin.
I have set threshold to 1 and set the focus to autocomplete view in onCreate event of activity.
autoCompleteView.FocusChange += delegate(object sender, View.FocusChangeEventArgs args)
{
if (args.HasFocus)
{
autoCompleteView.ShowDropDown();
}
};
It displays the dropdown with all the suggestions when my app is loaded. Whenever i type text, it filters based on condition and everything is working fine. But when i clear all the text in the autocomplete view, the dropdown is closed. But i want to display the dropdown with all the suggestions.
Also whenever i touch/click the autocomplete view, the dropdown is closed. So i have added the below code to display the dropdown, but there is a flicker(dropdown is closed and is opened again).
autoCompleteView.Click += delegate(object sender, EventArgs args) {
autoCompleteView.ShowDropDown ();
};
Thanks in advance.
It displays the dropdown with all the suggestions when my app is
loaded
You can call autoCompleteView.ShowDropDown(); in you OnCreate (independant of focus), this way the app loads with the dropdown showing.
Also whenever i touch/click the autocomplete view, the dropdown is
closed. So i have added the below code to display the dropdown, but
there is a flicker(dropdown is closed and is opened again).
Subscribe to the TextChanged event and call autoCompleteView.ShowDropDown(); whenever the text in the EditText is String.Empty.
I have a TextBox in my app, and an ApplicationBarIconButton in the ApplicationBar which acts as a "submit" for the contents of the TextBox.
When editing the TextBox using the virtual keyboard, the ApplicationBarIconButton is still visible below the SIP, so you can submit straight away without dismissing the keyboard: nice!
However, when clicking the button, the viewmodel to which the TextBox is bound does not update.
I found someone else with the same problem here, and they have used the pretty nasty workaround of manually updating the viewmodel on the TextBox's TextChanged event.
Removes all the elegance of using databound view models!
Is this a bug in WP7?
Or is there a nicer way around this that I haven't found yet?
The problem is that silverlight bindings do not support the PropertyChanged value for UpdateSourceTrigger. This means that by default a TextBox will update the property bound to Text when the TextBox loses focus and the only other possibility is to update it explicitly in code as is done in the example from your link.
You only really have two options here: Update the binding when the button is clicked or remove focus from the TextBox when the button is clicked.
I usually update the binding on the TextChanged event. I use an extension method to do this:
public static void UpdateBinding(this TextBox textBox)
{
BindingExpression bindingExpression =
textBox.GetBindingExpression(TextBox.TextProperty);
if (bindingExpression != null)
{
bindingExpression.UpdateSource();
}
}
allowing me to just call this in code behind:
textBox.UpdateBinding();
You may also be able to use a custom behaviour for this.
Summary of the steps to allow binding to work for each keypress of a textbox, instead of just when the text box loses focus. Uses Prism. It is a slightly indirect solution to the original problem.
In NuGet package manager, search for Prism. Add "Prism.Phone" created by "Microsoft patterns & practices"
Add the following to the page's phone:PhoneApplicationPage tag
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:prismInteractivity="clr-namespace:Microsoft.Practices.Prism.Interactivity;assembly=Microsoft.Practices.Prism.Interactivity"
Give your textbox a separate closing tag, and add the following between the opening and closing TextBox tags
<i:Interaction.Behaviors>
<prismInteractivity:UpdateTextBindingOnPropertyChanged/>
</i:Interaction.Behaviors>
I guess this will work but you should check if it is really necessary having an ApplicationBarIconButton (or simply a button on the page) for that.
Often you should avoid this when you like to have a good Metro design in your app you may prefer using InputScope="Search" + Hiding the SIP is easily done using Page.Focus()
e.g. (an old article; InputScope="Search" worked for me)
http://4mkmobile.com/2011/02/wp7-devs-stop-adding-search-buttons/
See also: http://forums.create.msdn.com/forums/p/70506/619517.aspx#619517
private void SearchTextBox_KeyUp(object sender, KeyEventArgs e) handler:
Using InputScope="Search" for my search field
Using DataBinding Mode=TwoWay
Focus(); // hides the SIP
UpdateBinding(SearchTextBox); // the trick mentioned in here
App.ViewModel.ExecuteSearch();
Works fine in my MVVM app.
I am using a listbox in a data template - and from an earlier post I cannot reference the listbox directly in the code behind.
As a result I am capturing the last selected object in the selectionchanged event for the listbox and using this when I want to navigate.
I now need to also clear the selected object in the listbox -can I do this in the selectionchanged event (after storing it away).
Alternatively I could use the MouseLeftButtonDown event on the listbox (which I understand is the equivalent of a 'click') but can I get the selected object in the listbox in this event.
thanks
In the selection changed event set <ListboxName>.SelectedIndex = -1;
Also, do not use the MouseLeftButtonDown event. This will fire whenever the user touches the ListBox, even if they're just trying to scroll up / down and not actually selecting an item.
If you can't change the SelectedIndex in code behind then, instead of detecting the SelectionChanged event you could detect a Tap event on the ListBoxItem.
I am implementing the Pivot control in Windows Phone 7.
I want to know which Pivot item is selected and active after the user does a swipe gesture.
How to detect the swipe gesture event related to Pivot control?
Which delegate method should I use ?
SelectionChanged is the event to wire up to for detecting Pivot item changes.
As kP suggests, SelectedIndex/SelectedItem are the properties you can access to see which item is currently selected.
Here's an example event handler to show the SelectedIndex each time it changes.
private void thisPivot_SelectionChanged(object sender, SelectionChangedEventArgs e) {
System.Diagnostics.Debug.WriteLine(thisPivot.SelectedIndex);
}
You could use the Pivot.SelectedItem method which is a getter and setter. This will allow you to return the current PivotItem the user is on. Alternatively, you could use the Pivot.SelectedIndex method if you just want to access the indexes of each PivotItem.
If you want to perform an action once the PivotItem has actually finished loading entirely and transition animation sequence is complete, take a look at the LoadedPivotItem event.