How Binding Class inside Class in Windows Phone - c#

My problem is this, i have a class Match:
Match.cs
public class Match
{
public int Id { get; set; }
public Team Team_1 { get; set; }
public Team Team_2 { get; set; }
public string Date { get; set; }
public string Hour { get; set; }
public string Local { get; set; }
public string Statium { get; set; }
}
and this class have Two objects Team, in Team i have this attributes:
Team.cs
public class Team
{
public int Id { get; set; }
public string Name { get; set; }
public string Image { get; set; }
...
im try Binding the Match object in my view:
<ListBox Name="lbMatches" Height="480" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Width="60" Orientation="Vertical">
<TextBlock Text="{Binding Date}" FontFamily="Segoe WP Semibold" />
<TextBlock Text="{Binding Hour}" FontFamily="Segoe WP Semibold"/>
</StackPanel>
<TextBlock VerticalAlignment="Center" Text="Brazil" FontSize="32" FontFamily="Segoe WP Semibold" Margin="20,0,10,0"/>
<Image Source="{Binding Thumb}"/>
<TextBlock VerticalAlignment="Center" Text="x" FontSize="32" Margin="10,0" Opacity="0.8"/>
<Image Source="/Assets/team_image/thumbs/croacia.png"/>
<TextBlock VerticalAlignment="Center" Text="Croatia" FontSize="32" FontFamily="Segoe WP Semibold" Margin="10,0,20,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
But have a crucial duvid, how i make to acess the Name of Team Binding a Match ? bind the team of Match how make this ?

You can navigate to the relationship from Match to Team in a binding, e.g.
<TextBlock Text="{Binding Team_1.Name}"/>

You did not set the ItemSource of your listbox. Next, you can access to Team attributes with : "Team_1.attribute"
Here an example :
xaml :
<ListBox Name="lbMatches" Height="480" ItemSource="{binding myMatch}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel Width="60" Orientation="Vertical">
<TextBlock Text="{Binding Team_1.Name}" FontFamily="Segoe WP Semibold" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Related

Binding expression path failure WP 8.1

I'm trying to get a test app that grabs data from an online Json file and data bind it after it is deserialized.
I'm receiving the following error for every data bind that I set:
Error: BindingExpression path error: 'field_class_day_value' property not found on 'JSON_Test_2.MainPage+Node, JSON_Test_2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. BindingExpression: Path='field_class_day_value' DataItem='JSON_Test_2.MainPage+Node, JSON_Test_2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String')
Here is the XAML code
<ListBox x:Name="scheduleList" Margin="10,0,30,0" Height="486" Width="404"
FontSize="20">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel Margin="10,0,10,8">
<TextBlock Text="{Binding field_class_day_value }"
TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding field_class_time_start_value }"
TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding field_class_time_end_value }"
TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding field_class_flex_header_value }"
TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding title }" TextWrapping="Wrap"
FontSize="18" />
<TextBlock Text="{Binding field_class_footer_value }"
TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding field_class_flex_footer_value }"
TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding field_class_studio_nid_1 }"
TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding field_class_instructor_nid }"
TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding field_ages_value }"
TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding field_class_header_value }"
TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding field_class_unique_price_value }"
TextWrapping="Wrap" FontSize="18" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Classes that were created for use with the Json:
public class Node2
{
public string field_class_day_value { get; set; }
public string field_class_time_start_value { get; set; }
public string field_class_time_end_value { get; set; }
public string field_class_flex_header_value { get; set; }
public string title { get; set; }
public string field_class_footer_value { get; set; }
public string field_class_flex_footer_value { get; set; }
public string field_class_studio_nid_1 { get; set; }
public string field_class_instructor_nid { get; set; }
public string field_ages_value { get; set; }
public string field_class_header_value { get; set; }
public string field_class_unique_price_value { get; set; }
}
public class Node
{
public Node2 node { get; set; }
}
public class RootObject
{
public List<Node> nodes { get; set; }
}
Finally the method that was created to deserialize the JSON file
async private void ReadDataFromWeb()
{
var client = new HttpClient(); // Add: using System.Net.Http;
var response = await client.GetAsync(new
Uri("url"));
string result = await response.Content.ReadAsStringAsync();
RootObject rootobject = JsonConvert.DeserializeObject<RootObject
(result);
scheduleList.ItemsSource = rootobject.nodes;
}
I think I'm receiving the data binding error because there is another object that is in between the rootobject and the actual data that I need to use. Is that what I'm missing or are my data binding statements incorrect?
Your path is not correct, as the error messages indicates. You're trying to bind to property field_class_day_value but your DataContext in the DataTemplate is node (as your ItemsSource is a list of nodes)
Text="{Binding field_class_day_value}"
It should be
Text="{Binding node.field_class_day_value}"
By the way: why do you wrap your node2 as a property in node?

Deserializing a JSON String to LongListSelector Windows Phone 8

I am working on windows phone app and I want to show some grouped JSON list item to longlist selector but I am not able to show anything in LongListSelector please help me following code I am using-:
Code Behind
public class UserInfo
{
public string service_name { get; set; }
public string uid { get; set; }
public string fname { get; set; }
public string lname { get; set; }
public string Contact { get; set; }
public object Mutual_friend { get; set; }
public object Direct { get; set; }
public string Miles { get; set; }
}
public class ResultSearch
{
public string service { get; set; }
public List<UserInfo> user_info { get; set; }
}
public class RootObjectSearch
{
public string flag { get; set; }
public string message { get; set; }
public List<ResultSearch> result { get; set; }
}
XAML
<phone:LongListSelector Name="test"
ItemsSource="{Binding service}"
HorizontalAlignment="Left"
Height="384"
Margin="10,136,0,0"
VerticalAlignment="Top"
Width="436">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Left"
Height="301"
Margin="10,10,0,0"
VerticalAlignment="Top"
Width="436"
Background="{StaticResource PhoneAccentBrush}">
<StackPanel Orientation="Horizontal"
Height="64">
<TextBlock Text="{Binding service }"
TextWrapping="Wrap"
Foreground="Red"
Margin="10"
Width="229"
FontSize="24" />
</StackPanel>
<StackPanel Orientation="Horizontal"
Height="64">
<TextBlock Text="{Binding fname }"
TextWrapping="Wrap"
Foreground="Red"
Margin="10"
Width="229"
FontSize="24" />
</StackPanel>
<StackPanel Orientation="Horizontal"
Height="64">
<TextBlock Text="{Binding lname }"
TextWrapping="Wrap"
Foreground="Red"
Margin="10"
Width="229"
FontSize="24" />
</StackPanel>
<StackPanel Orientation="Horizontal"
Height="64">
<TextBlock Text="{Binding Contact }"
TextWrapping="Wrap"
Foreground="Red"
Margin="10"
Width="229"
FontSize="24" />
</StackPanel>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
I assume that you are able to download the json as a string.
Once you have that string add Json.NET library via nuGet and then add:
List<UserInfo> userInfo = JsonConvert.DeserializeObject<List<UserInfo>>(response);
where response is the string with the json inside.
Ok, now link the List<UserInfo> to the LongListSelector:
longListSelector.ItemSource = userInfo;
where longListSelector is the x:name of the phone:LongListSelector you have previously created in XAML

Binding nested JSON to listbox in XAML

This is my modal,
public class main
{
public List<categories> categorieslist { get; set; }
}
public class categories
{
public int categoryId { get; set; }
public string categoryName { get; set; }
public List<pdf> pdfdocs { get; set; }
public List<video> videoFiles { get; set; }
}
public class pdf
{
public string URL { get; set; }
public string language { get; set; }
public string createdDate { get; set; }
public bool isFavorite { get; set; }
public bool isRead { get; set; }
}
Im using JSON.NET to deserialize
main mainobj = JsonConvert.DeserializeObject<main>(App.hello);
I need to display list of PDF's for a selected category,
Im using LINQ to filter that particular category, I am unable to bind the PDF list..
pdf.ItemsSource = App.mainobj.categorieslist.Where(i => i.categoryId.Equals(s));
<ListBox x:Name="pdf" Margin="0,0,0,363" ItemsSource="{Binding}" Foreground="White">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding pdf.URL}" Foreground="White"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox ItemsSource="{Binding categorieslist}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding categoryId }" FontSize="20" />
<ItemsControl ItemsSource="{Binding pdf}" Margin="0 20 0 0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Blue" BorderThickness="2">
<TextBlock Text="{Binding URL }" FontSize="20" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl ItemsSource="{Binding videoFiles}" Margin="0 20 0 0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="2">
<TextBlock Text="{Binding URL}" FontSize="20" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>

ItemControl with 2 sources

I have the following Classes:
public class agentes
{
[PrimaryKey]
public string id { get; set; }
public string idContinente { get; set; }
public string idCountry { get; set; }
public string title { get; set; }
public string desc { get; set; }
}
and
public class country
{
[PrimaryKey]
public string id { get; set; }
public string idContinent { get; set; }
public string title { get; set; }
}
i also have 2 other classes, and have a List of objects of each of those types , country and agents.
my List is filled with the results from a Database query to a sqlite database that looks something like this:
public List<agentes> GetAllAgents()
{
List<agentes> AllAgents = new List<agentes>();
using (var db = new SQLite.SQLiteConnection(app.DBPath))
{
var query = db.Table<agentes>().OrderBy(c => c.idCountry);
foreach (var _Agent in query)
{
var Agente = new agentes()
{
desc = _Agent.desc,
id = _Agent.id,
idContinente = _Agent.idContinente,
idCountry = _Agent.idCountry,
title = _Agent.title,
};
AllAgents.Add(Agente);
}
}
return AllAgents;
}
Now, what i intend to do , is a list of agents organized by country so it looks something like
i started with something like this but clearly it inst whats needed
<ScrollViewer Margin="30,30,0,30" Height="444">
<ItemsControl Name="ListCountries">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding title}" Foreground="Red" />
<ItemsControl Name="AgentsByCountry" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding desc}" Foreground="Black" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
anyone got any suggestions how i can do this?
You can use LINQ to group by title which will create IGrouping for each country where Key will be your country:
ListCountries.ItemsSource = GetAllAgents().GroupBy(a => a.title);
and then change binding for inner TextBlock and ItemsControl like so
<ScrollViewer Margin="30,30,0,30" Height="444">
<ItemsControl Name="ListCountries">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Key}" Foreground="Red"/>
<ItemsControl ItemsSource="{Binding}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding desc}" Foreground="Black" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>

Get Object properties of selected list item

I have a listbox that has a listsource that is a list of 'results' objects. The results class looks like this:
public class Result
{
public string GUID { get; set; }
public string __invalid_name__I { get; set; }
public string FN { get; set; }
public string DOB { get; set; }
public string SEX { get; set; }
public string SN { get; set; }
public string __invalid_name__U { get; set; }
public string TYPE { get; set; }
//Gender icon path associated with result
public string SexIcon { get; set; }
}
And this is what my listbox looks like in the xaml code:
<ListBox
Height="517"
HorizontalAlignment="Left"
Margin="12,84,0,0"
Name="searchList"
VerticalAlignment="Top"
Width="438"
SelectionChanged="SearchList_SelectedEvent">
<!-- What each listbox item will look like -->
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Path=Sex, Converter={StaticResource SexToSourceConverter}}" Visibility="Visible" />
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Name="FirstName" Text="{Binding FN}" FontSize="28" Margin="0,0,10,0"/>
<TextBlock Name="LastName" Text="{Binding SN}" FontSize="28" />
</StackPanel>
<TextBlock Text="{Binding DOB}" FontSize="24" />
<!-- Line Stroke="#FF04863C" StrokeThickness="3" X1="100" Y1="100" X2="300" Y2="100" / -->
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
So my question is, (and what I'm really struggling with) is how does one for instance get the value of the GUID property of the selected Item (which is basically a results object)?
(searchList.SelectedItem as Result).GUID

Categories