Cannot set Picker Default value - xaml

I have a picker on my Xamarin form, bound to a list, and I would like that it has the first item of that list already selected when the form is opening.
My list looks like this
Languages = new List<string>();
Languages.Add("English");
Languages.Add("Nederlands");
Languages.Add("русский");
I tried like this
<Picker x:Name="PickerLanguage" Title=""
TitleColor="#004973"
FontSize="24"
ItemsSource="{Binding Languages}"
SelectedIndex="0"
SelectedItem="English">
</Picker>
and like this
<Picker x:Name="PickerLanguage" Title=""
TitleColor="#004973"
FontSize="24"
ItemsSource="{Binding Languages}"
SelectedIndex="0"
SelectedItem="0">
</Picker>
Both attemps above are based on the answer of this question
But is does not works.
How can I have it so that when the form opens, English is set as selected item/index already ?

create a property for your selected item in your c# then bind it to your picker :
private string selectedObj;
public string SelectedObj
{
get{ return selectedObj;}
set{ selectedObj= value ;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedObj"));}
}
then you can set your value to the selected object any place in your code:
SelectedObj = "English";
then bind it to picker in xaml:
<Picker x:Name="PickerLanguage" Title=""
TitleColor="#004973"
FontSize="24"
ItemsSource="{Binding Languages}"
SelectedItem="{Binding SelectedObj}">
</Picker>

Related

ListView doesn't display data in xamarin

I am new to xamarin and trying to display a listview. My attempt is as follows.
<ContentPage.Content>
<StackLayout>
<ListView x:Name="ComListView" RowHeight="80">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid Margin="8">
<Label Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Grid.ColumnSpan="2" Text="hello world" FontAttributes="Bold" />
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
But when I ran the app, even I navigate to correct page; nothing display. For any other views, it will display the right output. So where could I get wrong on the listview?
You have to put items to the ListView. Currently, you only specify the ItemTemplate, which determines how an item will look (in your case, they will all look the same (i.e. Labels with 'hello world' text)). But the ListView is empty and so there are no items to show.
You can set items using the ItemsSource property of the ListView.
Usually, you use Binding inside an ItemTemplate to show item content.
Let's say we have a class Person:
class Person
{
public string Name { get; set; }
}
Then you create a List of Person objects and add it to your ListView in code:
var personnel = new List<Person>
{
new Person { Name = "Booker" },
new Person { Name = "Elizabeth" }
};
ComListView.ItemsSource = personnel;
And in your XAML, you have to set the ItemTemplate so that it shows the Name of each Person:
<Label Text="{Binding Name, Mode=OneWay}" />
Note: You can also bind to ItemsSource from XAML if you have a ViewModel.
You are not binding the data to the listview.
use the ItemSource property to bind data, either from xaml, code behind or use a binding to populate from your ViewModel

Xamarin.Froms MarkupExtension used in Picker.Items

I've written an markup extension for translating strings using the resources.
[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
private readonly ILocalizationService _localizationService;
public string Text { get; set; }
public TranslateExtension()
{
_localizationService = Mvx.Resolve<ILocalizationService>();
}
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Text == null)
return "";
return _localizationService.GetString(Text);
}
}
It's working fine if I use it in Attributes of XAML Elements like:
<Label Text="{i18n:Translate DebugView}" VerticalOptions="Center" HorizontalOptions="Center" />
But now I want to provide a list of strings in Picker.Items.
<Picker SelectedIndex="0" HorizontalOptions="End" VerticalOptions="Center" Title="{i18n:Translate State}">
<Picker.Items>
<i18n:TranslateExtension Text="OnState"></i18n:TranslateExtension>
<i18n:TranslateExtension Text="OffState"></i18n:TranslateExtension>
</Picker.Items>
</Picker>
I don't get it to work. Only if I use one TranslateExtension element within Picker.Items, it's correctly shown. Otherwise it crashes ('TranslateExtension' cannot be converted to type 'System.String'). I tried a lot of different styles of writing the elements, but I had no success. What is the correct way to write it?
<Picker SelectedIndex="0" HorizontalOptions="End" VerticalOptions="Center" Title="{i18n:Translate State}">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<i18n:TranslateExtension Text="OnState"></i18n:TranslateExtension>
<i18n:TranslateExtension Text="OffState"></i18n:TranslateExtension>
</x:Array>
</Picker.ItemsSource>
</Picker>
Try this

DataBind to Combobox In SilverLight 4 .0

I am trying to databind to the combobox. Data is coming from a database table which name is tbltest and table has 2 fileds id and name.
When I am trying to bind name to combox it display me tbltest:name in View. I am using domain services and MVVM to bind data.
Below is my code of ViewModel:
public ViewModel()
{
var query = context.GetTblTestsQuery();
var load = context.Load(query);
load.Completed += (s, ea) =>
{
ObsCompanyCollection = new ObservableCollection<tblTest>(context.tblTests);
};
}
private ObservableCollection<tblTest> _ObsCompanyCollection = new ObservableCollection<tblTest>();
public ObservableCollection<tblTest> ObsCompanyCollection
{
get
{
return _ObsCompanyCollection;
}
set
{
if (_ObsCompanyCollection != value)
{
_ObsCompanyCollection = value;
NotifyPropertyChanged("ObsCompanyCollection");
}
}
}
and Below is code of my XAml file:
<UserControl.Resources>
<my:ViewModel x:Key="ViewModel"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource ViewModel}">
<ComboBox Height="23" HorizontalAlignment="Left" Margin="47,128,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" DisplayMemberPath="{Binding name,Mode=TwoWay}" ItemsSource="{Binding ObsCompanyCollection,Mode=TwoWay}" SelectedItem="{Binding tbldata.SelectCompanyId,Mode=TwoWay}" />
I dont know what is wrong with this code. I want only name to display in my combobox.
Thanks
try this
<ComboBox Height="23" HorizontalAlignment="Left" Margin="47,128,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" DisplayMemberPath="name" ItemsSource="{Binding ObsCompanyCollection,Mode=OneWay}"

How to use trigger event in a button inside a datagrid in silverlight using relaycommand mvvm

How to use trigger event in a button inside a datagrid in silverlight using relaycommand mvvm
Iam unable to get selected values in to some Dto , it means once i selected a row for delete , the selected item property showing NULL .how to solve it pls
You can use trigger event like below in datagrid:
<Button Content="Message" Height="23" HorizontalAlignment="Left" Margin="234,116,0,0" Name="btnMsg" VerticalAlignment="Top" Width="75" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<si:CallDataMethod Method="HandleShowMessage"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
You have to add necessary reference for this.
For selecteditem you have to set selected item into datagrid and other thing you have to decalre a property in viewmodel:
In Xaml:
<sdk:DataGrid Height="Auto" AutoGenerateColumns="False" ItemsSource="{Binding Emp}" SelectedItem="{Binding SelectedEMp,Mode=TwoWay}" BorderThickness="1" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="auto">
and in Viewmodel:
private EmpInfo _selectedEMp;
public EmpInfo SelectedEMp
{
get { return _selectedEMp; }
set
{
_selectedEMp = value;
on("SelectedEMp");
}
}
Thanks

Silverlight ComboBox in DataGrid Binding SelectedItem problem

I have a combobox in datagrid.I use Silverlight 4.0 and MVVM.
My code works fine,unless when I removed a record from datagrid and add another one, the SelectedValue binding for combobox in added row doesnt work.
<sdk:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items, Mode=TwoWay}" Name="dataGrid2" >
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn Width="50*">
<sdk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Path=Products, Mode=OneWay}"
SelectedValue="{Binding Path=ProductId,Mode=TwoWay}"
DisplayMemberPath="ProductTitle"
SelectedValuePath="ProductId"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellEditingTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
Thanks
Found this piece of code on some site, it helped me in a similar Situation:
public class ComboBoxEx : ComboBox
{
protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
var bindingExpression = GetBindingExpression(SelectedValueProperty);
base.OnItemsChanged(e);
if (bindingExpression != null)
{
var binding = bindingExpression.ParentBinding;
SetBinding(SelectedValueProperty, bindingExpression.ParentBinding);
}
}
}