Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to binding one Object in xaml.

BuiManhBuiManh USMember ✭✭
edited December 2016 in Xamarin.Forms

In page 1, i have List then Binding it in ListView. I use ItemSelected to get Id of News then send it to Page 2.
public NewsPage()
{
InitializeComponent();
string s = GetNews().Result;
List<News> news = new List<News>();
news = JsonConvert.DeserializeObject<List<News>>(s);
ListViewNews.ItemsSource = news;
ListViewNews.ItemSelected += async (sender, e) =>
{
News str = (News)ListViewNews.SelectedItem;
await Navigation.PushAsync(new DetailPage(str.NewsId));
};
}
In page 2, i use id to get this News then show to page.

public DetailPage(int id)
{
InitializeComponent();
string s1 = GetDetialNews(id).Result;
News news1 = new News();
news1 = JsonConvert.DeserializeObject<News>(s1);
// ListDetail.ItemsSource = news1;
}

My DetailPage.xaml
<ListView x:Name="ListDetail">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding Title}" FontSize="Large" TextColor="#3498DB" LineBreakMode="NoWrap"/>
<Label Text="{Binding PublicDate}" FontSize="small" />
<Image Source="{Binding IMG}" VerticalOptions="Center">
</Image>
<StackLayout VerticalOptions="Center">
<Label Text="{Binding Body}" x:Name="body" FontSize="Small" LineBreakMode="WordWrap"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

But I cant use listView becase news1 is not list. How can i Binding news1.

Best Answers

Answers

  • prashantvcprashantvc USXamarin Team Xamurai

    news1 is the single instance of the News object so you cannot bind it to the listview's itemsource.
    Instead set it to the DetailsPage's binding context and then bind the properties to the regular controls like Label & Image

  • BuiManhBuiManh USMember ✭✭
    edited December 2016

    @prashantvc said:
    news1 is the single instance of the News object so you cannot bind it to the listview's itemsource.
    Instead set it to the DetailsPage's binding context and then bind the properties to the regular controls like Label & Image

    How to send data news1 to Label and IMG. ListView have x:Name and I set news1 to this x:Name then Label and IMG can binding this. Which xamarin form I set news1 to.???
    Can u give me some example code.

Sign In or Register to comment.