I have these classes:
public class MovieExt
{
public string Title { get; set; }
public string Year { get; set; }
public List<string> Genres { get; set; }
public List<Actor> Actors { get; set; }
....
}
public class Actor
{
public string Name { get; set; }
public string Birth { get; set; }
public string Biography { get; set; }
public string Url { get; set; }
}
and this is my method in my page:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
object obj;
if (PhoneApplicationService.Current.State.TryGetValue("movie", out obj))
{
MovieExt movie = (MovieExt)obj;
this.DataContext = movie;
this.imgPoster.Source = new BitmapImage(new Uri(movie.PosterUrl, UriKind.Absolute));
}
base.OnNavigatedTo(e);
}
and in page I am binding properties like this:
<ListBox Grid.Row="4" Grid.Column="1"
Margin="5,5,5,5"
ItemsSource="{Binding Path=Actors }"
x:Name="listStars"/>
For everything it´s working (genres and others). Everything else is string. But for actors I want to bind in list name and after clicking on the actor I want to go to url. How can I bind name property from actor? Thanks
First of all, you need to create OnSelectedItemChanged event on your ListBox to handle clicks on your Actors.
Then you need to get your clicked item. You can do this in several ways. The simplest way is listBox.SelectedItem property.
And then your can get your Url with (listBox.SelectedItem as Actor).Url
Also, when you go back from details page, SelectedItem will be not null and click on the same item not fired event in second time. So, set SelectedItem to null when click is handled
UPD: to properly bind Actor class to ListBox you need to create ItemTemplate:
<ListBox ...>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text={Binding Name} />
<TextBlock Text={Binding Birth} />
<TextBlock Text={Binding Biography} />
<TextBlock Text={Binding Url} />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You can override the ToString() method in the Actor class to display something friendly, like the name.
public override string ToString()
{
return Name;
}
This is useful when binding objects to comboboxes and dropdowns.
Related
I have ListView with Button showing status A(Absent), P(Present) and L(Late) for each item. On Button click status Text getting changed P to A, A to L.
When changing status of any item, I have to change AttendanceId, AttendanceTypeStatusCD, AttendanceTypeStatusId fields for for the same record. This I have to do for multiple records. I need to update my ListView's source StudentList according to action performed and post modified data to db.
<ListView x:Name="list1" ItemsSource="{Binding StudentList}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Image Source="other.png" />
<Label Text="{Binding StudentName}" />
<Button x:Name="mybtn"
BindingContext="{Binding Source={x:Reference list1}, Path=BindingContext}"
Command="{Binding BtnTextCommand}"
CommandParameter="{Binding Source={x:Reference mybtn}}"
Text="{Binding AttendanceTypeStatusId, Converter={x:StaticResource IDToStringConverter}}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
This is my model class
public class Student
{
public int? StudentId { get; set; }
public string StudentName { get; set; }
public int? AttendanceTypeStatusId { get; set; }
public string ClassLevelCd { get; set; }
public int? StudentDailyAttendanceId { get; set; }
public int? AbsentReasonId { get; set; }
public string AttendanceTypeStatusCD { get; set; }
public string Name { get; set; }
public string LoggedInUser { get; set; }
public string ImageValue { get; set; }
public string FirstName { get; set; }
}
This is the method in ViewModel in to change specific item text
public void SetBtnText(object sender)
{
var view = sender as Xamarin.Forms.Button;
if (view.Text == "P")
{
view.Text = "A";
AbsentReasonId = "3001";
AttendanceTypeStatusCD= "Absent";
AttendanceTypeStatusId = "3002";
}
else if (view.Text == "A")
{
view.Text = "L";
AbsentReasonId= "2001";
AttendanceTypeStatusCD = "Late";
AttendanceTypeStatusId = "2002";
}
else
{
view.Text = "P";
AbsentReasonId = "4001";
AttendanceTypeStatusCD = "Present";
AttendanceTypeStatusId = "4002";
}
}
//Above used properties
private string _absentReasonId;
public string AbsentReasonId
{
get { return _absentReasonId; }
set { SetProperty(ref _absentReasonId, value); }
}
private string _attendanceTypeStatusCD;
public string AttendanceTypeStatusCD
{
get { return _attendanceTypeStatusCD; }
set { SetProperty(ref _attendanceTypeStatusCD, value); }
}
private int _attendanceTypeStatusId;
public int AttendanceTypeStatusId
{
get{return _attendanceTypeStatusId;}
set { SetProperty(ref _attendanceTypeStatusId, value); }
}
The above process is not working, SutdentList source not getting changed, How can I do that using MVVM?
Student list screenshot
Need some direction/help here guys. Similar to a basic contacts app - I'm building a customer entry screen and need a drop down for contact types (phone, email, etc). I figured out how to populate the picker(s) by explicitly defining the path and source but I can't seem to get SelectedItem to do anything. My expectation is set the default value of the picker with whats in the record and also be able to change that value.
<ListView x:Name="ContactListView" ItemsSource="{Binding Customer.ContactList}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout>
<Picker
ItemsSource="{Binding Path=BindingContext.ContactTypes, Source={x:Reference ContactListView} }"
ItemDisplayBinding="{Binding TypeName}"
SelectedItem="{Binding TypeName, Mode=TwoWay}"
/>
<Entry Text="{Binding Value}" />
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The ViewModel attached to this page is a Customer object which has a nested contact list in it (defined at the list view). Below is a basic version of the Customer Class that I'm using on the page with its relationship to ContactTypes.
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string AddressLine { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public List<Contact> ContactList { get; set; }
}
public class ContactType
{
public string TypeName { get; set; }
public int SortOrderId { get; set; }
}
public class Contact : ContactType
{
public int Id { get; set; }
public string Value { get; set; }
}
When the Page ViewModel loads, I store a list of possible ContactTypes (one time) that the user can choose from and it's shared in each picker as the item source. The list of possible ContactTypes from the database are stored in a local variable - here's the static representation of it:
public IList<ContactType> ContactTypes { get; } = new List<ContactType>
{
new ContactType(){ TypeName = "Home Phone", SortOrderId = 1 },
new ContactType(){ TypeName = "Cell Phone", SortOrderId = 2 },
new ContactType(){ TypeName = "Work Phone", SortOrderId = 3 },
new ContactType(){ TypeName = "Email", SortOrderId = 4 },
new ContactType(){ TypeName = "Fax", SortOrderId = 5 }
};
I need the picker to show the options from ContactTypes, and when an Item is Selected, it updates the TypeName property of the current record iteration in ContactList.
I am trying to bind the values for a picker from a model in view model, there I am getting path of the model instead of the values.
<Picker x:Name="LocationPicker"
Title="Location" HeightRequest="40"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
SelectedItem="{Binding Location}"
ItemsSource="{Binding MaintainRoomTypes}"/>
Here is my view model code:
if (jobDetailsForFitter != null)
{
WindowDetails = new WindowDetailsModel
{
Windows = jobDetailsForFitter.Windows,
Locations = jobDetailsForFitter.Locations,
RoomTypes = jobDetailsForFitter.RoomTypes,
AddFiles = jobDetailsForFitter.AddFiles
};
Locations = jobDetailsForFitter.Locations;
MaintainRoomTypes = jobDetailsForFitter.RoomTypes;
await FitterService.Instance.LoadJobDetailsToLocalStore(jobDetailsForFitter, SelectedJob?.Id ?? 0);
}
how to bind itemsource to get list.
public List<Room> Locations { get; set; }
public List<RoomTypeModel> RoomTypes { get; set; }
You have to define ItemDisplayBinding property to Picker.
For eg:
public class Room
{
public string RoomNumber { private set; get; }
public string RoomName { private set; get; }
}
And you want to display RoomName in Picker
<Picker ItemsSource="{Binding Room}" ItemDisplayBinding="{Binding RoomName}"/>
Hope this will solve your problem.
Iam trying to bring the values of static variable from the following class
public class LoginUserId
{
public static int id { get; set; }
public static string username { get; set; }
}
from here i created another class to get values as following
public class UsersList
{
public static List<LoginUserId> Names { get; set; }
}
Now i have xaml page as
<ListView ItemSelected="LoginUserList_OnItemSelected" ItemsSource="{Binding Source={x:Static lacal:UsersList.Names}}" IsVisible="True" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding username}" TextColor="Black" FontSize="20" IsVisible="True"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
At output iam not getting anything just a blank page is appearing,please correct me where i went wrong.
You can't bind to static variables with the way you did it in your XAML. When you use Text="{Binding username}" your property has to be a non static Property public string username { get; set; } if you want to bind to your static property then you have to use the x:Static way.
<Label Text="{x:Static local:YourViewModel.username}" />
of course you have to add a namespace of your viewModels to the ContentPage
modify your code as below
public class LoginUserId
{
public int id { get; set; }
public string username { get; set; }
}
public class UsersList
{
public static List<LoginUserId> Names = new List<LoginUserId>() {
new LoginUserId{id = 1, username = "No1"},
new LoginUserId{id = 2, username = "No2"},
};
}
The List Names must be static , but the property of object in it should not be static , because we must initalize the variable for the model and assign the value for the properties, it's not steady.
I'm working on windows phone 8 application, which should work in two languages, English and Arabic.
The default language is English.
Later User can change the language either from English to Arabic or from Arabic to English in Settings page.
When user clicks on "Cities" button in the Home Page, I'm displaying the cities in listbox.
By default i'm binding the city to english value i.e. TextBlock x:Name="city" Text="{Binding CityNameEn}".
Below is the UI code.
<ListBox x:Name="citiesList" SelectionChanged="citiesList_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Height="50" Margin="0,10,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
<StackPanel x:Name="dataRow" Grid.Row="0" Orientation="Horizontal">
<TextBlock x:Name="city" Text="{Binding CityNameEn}" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="28" Width="420"/>
<Image x:Name="arrow" Stretch="Fill" Margin="0,0,0,0" Source="Images/arrow.png" Height="20"/>
</StackPanel>
<Image x:Name="line" Grid.Row="1" Width="460" HorizontalAlignment="Center" Source="Images/separator.png" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
I'm setting the list box source like below.
private void Cities_Page_Loaded(object sender, RoutedEventArgs e)
{
citiesList.ItemsSource = Constants.cities;
}
Below is the Data Context class of City.
class City
{
public string CityId { get; set; }
public string CityNameAr { get; set; }
public string CityNameEn { get; set; }
public string DisplayOrder { get; set; }
public int FocusedCityIndex { get; set; }
}
Now When the language is English, then all the cities are displaying in English.
Because we bind the text block to property CityNameEn which contains english values.
When user changes the language in Settings Page from English to Arabic.
Then I implemented the localization like below.
if (selectedItem.Equals("English"))
{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
}
else
{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ar");
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ar");
}
Now When user clicks on the cities button in Home Page, then all the cities sholud display in Arabic.
*Because user changed language from English to Arabic.*
But cities are displaying in English only because textblock binded to the property which contains English value.
*So to display the cities in Arabic, I've to change the textblock binding to the property which contains Arabic value.*
How should I change the binding property of text block based on the language change.
Thanks.
You could add a City.CityName getter property in your City class that will return the city name based on the current culture, like this:
class City
{
public string CityId { get; set; }
public string CityNameAr { get; set; }
public string CityNameEn { get; set; }
public string CityName
{
get
{
if (Thread.CurrentThread.CurrentCulture.Name == "en-US")
return this.CityNameEn;
else
return this.CityNameAr;
}
}
public string DisplayOrder { get; set; }
public int FocusedCityIndex { get; set; }
}
Then use that property in your XAML:
<TextBlock x:Name="city" Text="{Binding CityName}" Foreground="#FF501F6E" Style="{StaticResource PhoneTextNormalStyle}" HorizontalAlignment="Left" FontSize="28" Width="420"/>