Windows phone user control change pivot selecteditem - windows-phone-7

I created my own UserControl, I added Button to it. Also I have Pivot where I added this UserControl. How can I change Pivot selecteditem in Button_Click event in my UserControl.
Binding:
Binding binding = new Binding("SelectedIndex");
binding.Source = historyControls[i].SelectedIndex;
Pivot_Second.SetBinding(Pivot.SelectedIndexProperty, binding);

void Button_Click()
{
SelectedIndex = desiredindex;
}
Make a property
private int _SelectedIndex;
public int SelectedIndex
{
get
{
return _SelectedIndex;
}
set
{
_SelectedIndex = value;
RaisedPropertyChanged("SelectedIndex");
}
}
then bind this property with selected index of your pivot.
I hope it will work.

Related

xamarin picker SelectedItem returning null on observable collection

in my xamarin project, picker binding, the SelectedItem is not working. When I have the ItemSource set to a List, the SelectedItem works, but when I change the ItemSource to an ObservableCollection, the SelectedItem always returns null. Can someone see what I am doing wrong?
on loading the view, the pickers are populated through databinding. then on a button event I try and grab the SelectedItem.... which is when it is coming back as null.
xaml
<Picker x:Name="PickerMarket2" Title="Market2" ClassId="PickerMarket2"
ItemsSource="{Binding TestList2}"
ItemDisplayBinding="{Binding ShortDesc}"
SelectedItem="{Binding SelectedMarket}"
Grid.Row="0" Grid.Column="1" >
</Picker>
view model
class VamiMarketViewModel: INotifyPropertyChanged
{
private List<string> _testList;
public List<string> TestList
{
get { return _testList; }
set
{
{
_testList = value;
NotifyPropertyChanged();
}
}
}
private ObservableCollection<Performance> _testList2;
public ObservableCollection<Performance> TestList2
{
get { return _testList2; }
set
{
{
_testList2 = value;
NotifyPropertyChanged();
}
}
}
private string _selectedMarket;
public string SelectedMarket
{
get { return _selectedMarket; }
set
{
{
_selectedMarket = value;
NotifyPropertyChanged();
}
}
}
I just explained the same in your other question here.
To what I see from your code, the SelectedItem seems to be the problem.
Since your Picker's ItemsSource(TestList property) is of type List<Performance>, the SelectedItem property bound to the Picker must be of type Performance. But, in your case, you have kept it as string instead of Performance.
The ItemDisplayBinding must be the name of any property inside your Performance object which in your case is fine since you have a string property called ShortDesc inside your Performance class.
That's the problem I see in your code. Change the type of the property ShortDesc like below and assign any one of the items in your collection TestList to it. Your code will start working fine.
private Performance _shortDesc;
public Performance ShortDesc
{
get { return _shortDesc; }
set
{
{
_shortDesc = value;
NotifyPropertyChanged();
}
}
}
Refer to the documentation here which explains a clear example for Binding objects to Picker.
I hope that helps.

Xamarin.Android / MvvmCross : Navigation with MvxSpinner

I have an issue with an Mvvmcross / Xamarin application in droid part.
I have made a "MvxSpinner menu" wich is bind on a list of couple
ViewModel.cs
private List<CoupleIntString> _actions = new List<CoupleIntString>() {
new CoupleIntString(0,"Actions"),
new CoupleIntString(1, "Mail"),
new CoupleIntString(2,"Imprimer"),
new CoupleIntString(3, "Totaux"),
new CoupleIntString(4, "Fiche client")
};
public List<CoupleIntString> Actions {
get { return _actions; }
set {
_actions = value;
RaisePropertyChanged(() => Actions);
}
}
droid.axml
<MvxSpinner
android:id="#+id/action_spinner"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
local:MvxItemTemplate="#layout/item_spinner"
local:MvxDropDownItemTemplate="#layout/item_spinnerdropdown"
local:MvxBind="ItemsSource Actions;SelectedItem ActionSelected" />
When I select one item, I set the SelectedAction of my FirstViewModel and show the viewmodel I want to load.
public CoupleIntString ActionSelected {
set {
int xx = value.intPart;
switch (xx) {
case 1: //mail
GoToMailCommand.Execute();
break;
case 2: //impression
GoToImpressionCommand.Execute();
break;
case 3: //totaux
GoToTotauxCommand.Execute();
break;
case 4: //impression
GoToDetailsClientCommand.Execute();
break;
default:
break;
}
}
But, when i come back on the FirstViewModel, it automatically re-set the SelectedAction and go back to the second Viewmodel.
I have tried to set my SelectedAction to none in Init, ReloadState, Start, InitFromBundle and ReloadFromBundle but after all these calls, there is another one with the value I selected before and I don't know where it comes from.
I would propose to add a SelectedItem listener and set SelectedItem to 0.
var spinner = FindViewById<MvxSpinner>(Resource.Id.action_spinner);
spinner.ItemSelected += Spinner_ItemSelected;
MvvmCross has a SelectedItem target binding for MvxSpinner, you are almost right with your code. However, you need to use a command instead of a property for the SelectedItem binding to work:
private MvxCommand<CoupleIntString> _itemSelected;
public ICommand ItemSelected =>
_itemSelected = _itemSelected ??
(_itemSelected = new MvxCommand<CoupleIntString>(DoItemSelected));
private void DoItemSelected(CoupleIntString item)
{
ActionSelected = item;
}
Then make your binding look like:
local:MvxBind="ItemsSource Actions; SelectedItem ItemSelected"

Checked checkboxes is unchecked when i scroll listview in xamarin android

Below is my GetView() method in adapter class, when I scroll the list view by selecting one checkbox. After scrolling back to initial position checked checkbox is getting unchecked.
public override View GetView (int position, View convertView, ViewGroup parent)
{
View view = convertView;
var item = mMyList [position];
//View holder
MyViewHolder holder = null;
if (view == null) {
holder = new MyViewHolder ();
view = mcontext.LayoutInflater.Inflate (Resource.Layout.listview_layout, null);
holder.mChecked = view.FindViewById<CheckBox> (Resource.Id.chkBox);
holder.Name = view.FindViewById<TextView> (Resource.Id.Name);
holder.StartDate = view.FindViewById<TextView> (Resource.Id.startDate);
holder.EndDate = view.FindViewById<TextView> (Resource.Id.endDate);
holder.Desc = view.FindViewById<TextView> (Resource.Id.Desc);
view.SetTag (holder);
} else {
holder = (MyViewHolder)view.Tag;
}
holder.Name.SetText (item.Name, TextView.BufferType.Normal);
holder.StartDate.SetText (item.StartDate, TextView.BufferType.Normal);
holder.EndDate.SetText (item.EndDate, TextView.BufferType.Normal);
holder.Desc.SetText (item.Description, TextView.BufferType.Normal);
return view;
}
It's simple!
You just need to store this "states" of checkboxes,because everytime when you scroll your listview, GetView method will be called(to draw hided items, Android reuses rows).
In your DataContext, for e.g. List<MyClass> , where MyClass represents:
public class MyClass
{
public string Name {get;set;}
public string SecondName {get;set;}
public bool IsChecked {get;set;}
}
try to add bool property(in this case IsChecked) for state of Checkbox.
And after this,in your GetView method write something likes this:
holder.mChecked.Checked = MyList [position].#YourBoolProperty#;
Btw i just write that on the fly.
Also,if something isn't clear for you, try to check this or this.
Hope that helps!

Create a Layout Item for ListView in Xamarin Android

I have a problem and It's 10 days that I am working and can't solve it.I made a layout for each row for ListView.This Layout Contains a linearLayout that there is a TextView and a WebView inside it.Now I Need a C# Project that I can add a new Row to the ListView with new text and url whenever I want.For Example: button.click { ListView.add(Resource.Layout.Items, "Text","Url")}..I know this command is wrong. Just I wanted to clear the problem for you.
I khnow it's custom Row layout and I read manny examples at this site other sites and Xamarin site about that,adapters,... but I can't do it. :(
Please answer me correctly.
It is very important for me.
Thanks a lot.
You need to create an adapter that can work with you custom objects as items. It could look like the following sample:
public class MyAdapter : BaseAdapter<MyItem>
{
readonly LayoutInflater inflater;
List<MyItem> myItemList;
public MyAdapter(Context context)
{
inflater = LayoutInflater.FromContext(context);
myItemList = YOUR_DATASOURCE.GetMyItems();
}
public override MyItem this [int index]
{
get { return myItemList[index]; }
}
public override int Count
{
get { return myItemList.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view = convertView ?? inflater.Inflate(Resource.Layout.MyItemLayout, parent, false);
var item = myItemList[position];
var viewHolder = view.Tag as MyViewHolder;
if (viewHolder == null)
{
viewHolder = new MyViewHolder();
viewHolder.Web = view.FindViewById<WebView>(Resource.Id.MyItemLayout_Icon);
viewHolder.Name = view.FindViewById<TextView>(Resource.Id.MyItemLayout_Title);
view.Tag = viewHolder;
}
viewHolder.Web.Url = item.Url; //You need to check how you have to set the url for a WebView
viewHolder.Name.Text = item.Text;
return view;
}
public override void NotifyDataSetChanged()
{
myItemList = YOUR_DATASOURCE.GetMyItems();
base.NotifyDataSetChanged();
}
}
class MyViewHolder : Java.Lang.Object
{
public WebView Web { get; set; }
public TextView Name { get; set; }
}
You apply the adapter to your ListView with ListView.Adapter = new MyAdapter(Activity);. Each time you change an item in you button click event, you tricker (ListView.Adapter as MyAdapter).NotifyDataSetChanged(); which will force the adapter to reload and refresh the data.
YOUR_DATASOURCE represents the point in your code where you store the informations like the url or text of all your items. This could typically be a database or something similar. While GetMyItems() is a method for example to query your database.
Hope this clears things up.

How to implement Hold in Listbox?

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;

Resources