Xamarin Forms Bind List<myObject> to Picker - c#

I have a problem. I created this Picker:
<Picker SelectedIndex="0" x:Name="pickerMode" Margin="30,0,30,0" />
And in my background code I set the ItemSource like this:
List<Mode> modeList = new List<Mode>();
Mode mode = new Mode
{
Id = 0,
Name = "Item1",
Type = "Test"
}
modeList.Add(mode)
pickerMode.ItemsSource = modeList;
But when I launch the App, the picker stays empty.
I want the Picker to show the Name of every Mode in that List.
What am I doing wrong?

Add this property in the XAML on your Picker ItemDisplayBinding="{Binding Name}"

Related

xamarin forms how to populate picker from code behind

Evening All,
Learning Xamarin forms..attempting to add a picker with numeric values...(using https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/picker/populating-itemssource)
I have used the example on this page to get the picker populated from the view...which works fine...however I want to populate the picker from code behind...
<---XAML--->
<Picker Grid.Column="4" Grid.Row="2" ItemsSource="{Binding pickerSource}"/>
<---c#---->
var pickerList = new List<string>();
pickerList.Add("1");
pickerList.Add("2");
pickerList.Add("3");
pickerList.Add("4");
pickerList.Add("5");
pickerList.Add("6");
pickerList.Add("7");
pickerList.Add("8");
pickerList.Add("9");
pickerList.Add("10");
var pickerSource = new Picker { Title = "Quantity", TitleColor = Color.Red };
pickerSource.ItemsSource = pickerList;
Picker is appearing on app but when selected, its not populated with any values...why isnt this binding properly anyone?
Thank you
Also...as a side note if anyone is aware of a tool that contains all numeric values instead of me manually having to populate it with 1,2,3 etc..
Thanks Again
Thanks to #Jason for the reply...From here I have went with the following:
---xaml--
<Picker Grid.Column="4" Grid.Row="2" ItemsSource="{Binding pickerSource}"/>
---c#----
public List<string> pickerSource { get; set; }
public void PopulateQuantityPicker()
{
var pickerList = new List<string>();
pickerList.Add("1");
pickerList.Add("2");
pickerList.Add("3");
pickerList.Add("4");
pickerList.Add("5");
pickerList.Add("6");
pickerList.Add("7");
pickerList.Add("8");
pickerList.Add("9");
pickerList.Add("10");
pickerSource = pickerList;
this.BindingContext = this;
}
The picker is on the app, but it is not populated, it is empty.
When I Click on it I get the following:
(also the code is hitting the PopulateQuantityPicker())
here you are binding your ItemsSource to pickerSource
<Picker Grid.Column="4" Grid.Row="2" ItemsSource="{Binding pickerSource}"/>
in your code behind, you need a public property named pickerSource. You can only bind to public properties
public List<string> pickerSource { get; set }
// assign the data to your ItemsSource
pickerSource = pickerList;
// also be sure to set the BindingContext
BindingContext = this;
// this is creating a new picker named pickerSource. You have already done
// this in your XAML. This is NOT NEEDED
var pickerSource = new Picker { Title = "Quantity", TitleColor = Color.Red };
pickerSource.ItemsSource = pickerList;
if you want to do this from the code behind WITHOUT using binding, you first need to assign an x:name to your control
<Picker x:Name="myPicker" Grid.Column="4" Grid.Row="2" />
then in the code behind
myPicker.ItemsSource = pickerList;

Updating UWP binding programmatically

I've got an issue where if an item is selected in a list I want it to update my items in my grid. The binding is done by:
<ScrollViewer Grid.Row="1">
<ItemsControl x:Name="RightGridItemsControl" ItemsSource="{Binding News}" ItemTemplate="{StaticResource RightGridTemplate}"/>
</ScrollViewer>
When an item, e.g. Planet is selected, I want to update the ItemsSource binding to a new list. This is specified in my DataModel.
How can I update this programmatically? I've tried something like this, but it requires a DependencyObject and can't find out what it means. This also looks like WPF rather than UWP.
`var myBinding = new Binding
{
Source = Planets,
Mode = BindingMode.OneWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
BindingOperations.SetBinding(new , ItemsControl.ItemsSourceProperty, myBinding);`
What should I put as the first item for the contstructor for 'SetBinding'?
You can set the Binding like this:
BindingOperations.SetBinding(
RightGridItemsControl, ItemsControl.ItemsSourceProperty, myBinding);
or like this:
RightGridItemsControl.SetBinding(ItemsControl.ItemsSourceProperty, myBinding);
Note also that currently there is no property path present in your Binding. If there is a News property as in your XAML, the Binding should probably look like shown below, without Mode = BindingMode.OneWay, which is the default, and without UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, which has no effect in a one-way binding.
var myBinding = new Binding
{
Source = Planets,
Path = new PropertyPath("News")
};

Data binding not giving expected results windows phone

In my windows phone app I've implemented data binding which is not yielding me expected results.
My functionality is I've a list box in which I've two textboxes which are data bound.
When I click the textbox datepicker/timepicker will open and the selected value should reflect in the textbox.
The xaml code for the listbox data template is as follows
<TextBox Visibility="{Binding TBVisibility}" IsReadOnly="{Binding TBReadOnly}" InputScope="{Binding Numeric}" AcceptsReturn="{Binding MultiLine}" Width="{Binding TBWidth}" TextWrapping="Wrap" Text="{Binding Path=TBText, Mode=TwoWay}" GotFocus="TextBox_GotFocus_1" KeyUp="TextBox_KeyUp_1" LostFocus="TextBox_LostFocus_1" />
<TextBox Visibility="{Binding TB2Visibility}" IsReadOnly="True" Width="140" Text="{Binding TB2Text, Mode=TwoWay}" GotFocus="TextBox_GotFocus_2" />
I'm launching the datepicker and timepicker as follows
private void LaunchDatePicker(TFDetails field)
{
datePicker = new CustomDatePicker
{
IsTabStop = false,
MaxHeight = 0,
Value = field.SelectedDate
};
datePicker.DataContext = field;
datePicker.ValueChanged += DatePicker_ValueChanged;
LayoutRoot.Children.Add(datePicker);
datePicker.ClickDateTemplateButton();
}
Where as "field" is the datacontext of the listbox.
The ValueChanged events are as follows
private void DatePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
DatePicker currentDP = sender as DatePicker;
TFDetails callingField = currentDP.DataContext as TFDetails;
if (callingField != null)
{
callingField.SelectedDate = currentDP.Value;
callingField.TBText = currentDP.ValueString;
}
}
When I change the time its not reflecting in the textbox. I wrote INotifyChangedProperty also.
May I know what mistake I could possible be doing here.
I actually have the same code in a similar UI page where it works perfectly. i don't know what mistake I'm doing here.
Thanks.
ListBox is a collection control. If you have a DataTemplate for it, bindings in it will use a single elements of collection boud to ItemsSource as a DataContext rather than DataContext od entire ListBox.

ListPicker TwoWay Binding and itemsource

I try to build settings page to my Windows Phone 8 app, the settings page has couple of ListPickers. The basic idea has been taken from here: http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff769510(v=vs.105).aspx
In my settings page xaml I have just declared:
<toolkit:ListPicker x:Name="listPicker1" ExpansionMode="FullScreenOnly" SelectionMode="Single" FullModeItemTemplate="{StaticResource generalListPickerFullTemplate}" ItemTemplate="{StaticResource generalListPickerTemplate}" SelectedItem="{Binding Source={StaticResource appSettings}, Path=listPicker1, Mode=TwoWay}" />
In "code behind", I create list and set it item source to listPicker1
listPicker1List.Add(new ListPickerItem() { name = "First value", value = "value_1" });
listPicker1List.Add(new ListPickerItem() { name = "Second value", value = "value_2" });
this.listPicker1.ItemsSource = listPicker1List;
StaticResource appSettings points to class which is basically similar than in the MS example,
public ListPickerItem listPicker1
{
get
{
return GetValueOrDefault<ListPickerItem>(KeyName, Default);
}
set
{
if (AddOrUpdateValue(KeyName, value))
{
Save();
}
}
}
So is it not possible to set itemsource and use two way bindig? If I set these both, I get System.ArgumentOutOfRangeException.
Basically my only goal is to have listPicker with items, which have text to display for user and value. And to easily set and get these to Isolates storage.
You have this error when you set itemsource or select an item?

WPF/C# Combobox Only Changes Selection When A Click is Detected Outside of Text Area

So I have several comboboxes in my WPF application that don't change the selection when a user clicks on the text of a combobox item. In order to select a particular item you have to click to the right or left of the text. I have another combobox that selects just fine when the text is clicked. The only difference between the two is databinding. The comboboxes that don't select when the text is clicked are databound to an ObservableCollection of one type or another. The combobox that works has manually inserted, static values.
I've searched the issue extensively and I can't seem to find anyone else who has had this issue or anything remotely similar. I'm not setting any weird properties.
Here is the code for one of the problematic comboboxes:
<ComboBox HorizontalAlignment="Left" Margin="40,160,0,0" VerticalAlignment="Top" Width="132" ItemsSource="{Binding Path=Systems}" SelectedItem="{Binding Path=System}" SelectedIndex="0">
<ComboBox.ItemTemplate>
<DataTemplate>
<ComboBoxItem Content="{Binding Path=Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Here is a video of the behavior:
http://www.youtube.com/watch?v=D0r1N1ghw-k
enter code hereSuppose my Combobox is defind as below
<ComboBox Name="cmb" Width="200" Height="20" DisplayMemberPath="PersonName" SelectedValuePath="PersonID">
</ComboBox>
please notice , i have removed the itemtemplate part
and my model is as below
public class Person
{
public string PersonName { get; set; }
public string PersonID { get; set; }
}
And my binding is in code behind , this step is not necessary , you can do it in the xaml , iam just checking quickly so much dirty code
public List<Person> source = new List<Person>();
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 20; i++)
{
source.Add(new Person() { PersonID = i.ToString(), PersonName = "Sau" + i.ToString() });
}
cmb.ItemsSource = source;
this.DataContext = this;
}
so if you run this sample , you will see you can select the value when you click on the text itself.
it's content property which causes some problem but i am not 100% sure on this.

Categories