Binding expression path failure WP 8.1 - c#

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?

Related

Create TreeView from binding (child node as List<MyType>) in MVVM

I need my TreeView to display playlists and their songs, it should look something like this:
-Playlist1
--Song1
--Song2
--Song3
-Playlist2
--Song1
--Song2
--Song3
--Song4
-Playlist3
--Song1
--Song2
There can be an unlimited amount of playlists in the TreeView, with each node having differing amounts of songs. I want to achieve this using MVVM.
MainWindow.xaml
<TreeView ItemsSource="{Binding PlayLists}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type types:Playlist}" ItemsSource="{Binding PlayLists}">
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type types:Song}">
<TextBlock Text="{Binding Path=Title}"/>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
Playlist.cs
public class Playlist
{
public string Name { get; set; }
public List<Song> Songs { get; set; }
public static ObservableCollection<Playlist> Playlists { get; set; } = new ObservableCollection<Playlist>();
}
Song.cs
public class Song
{
public string FullPath { get; set; }
public string Title { get; set; }
public string Artist { get; set; }
public string Genre { get; set; }
public string Length { get; set; }
public string Size { get; set; }
}
MainViewModel.cs
public ObservableCollection<Playlist> PlayLists { get; set; }
public MainViewModel()
{
/* Get Play-lists */
string listsPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AppName", "Playlists");
if (!Directory.Exists(listsPath))
{
Directory.CreateDirectory(listsPath);
}
string[] playlists = Directory.GetDirectories(listsPath);
foreach (var playlist in playlists)
{
var songs = new List<Song>();
string[] songsInPath = Directory.GetFiles(playlist);
foreach (var song in songsInPath)
{
songs.Add(NewSong(song));
}
Playlist newPlaylist = new Playlist()
{
Name = playlist.Split(Path.DirectorySeparatorChar).Last(),
Songs = songs
};
Playlist.Playlists.Add(newPlaylist);
}
PlayLists = Playlist.Playlists;
}
This is what it looks like:
There should be a sub-nodes for every song in the playlist.
The TreeView only shows the Playlist's Name, but not the Song's Title. I realized this is because the Playlist's property Songs is actually a List<Song>. I am not sure how I can display each Song's Title from the property Playlist.Songs.
Can someone please help me with this.
Your structure for Songs is wrong
Have a look at this example:
<HierarchicalDataTemplate DataType="{x:Type self:Family}" ItemsSource="{Binding Members}">
<StackPanel Orientation="Horizontal">
<Image Source="/WpfTutorialSamples;component/Images/group.png" Margin="0,0,5,0" />
<TextBlock Text="{Binding Name}" />
<TextBlock Text=" [" Foreground="Blue" />
<TextBlock Text="{Binding Members.Count}" Foreground="Blue" />
<TextBlock Text="]" Foreground="Blue" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type self:FamilyMember}">
<StackPanel Orientation="Horizontal">
<Image Source="/WpfTutorialSamples;component/Images/user.png" Margin="0,0,5,0" />
<TextBlock Text="{Binding Name}" />
<TextBlock Text=" (" Foreground="Green" />
<TextBlock Text="{Binding Age}" Foreground="Green" />
<TextBlock Text=" years)" Foreground="Green" />
</StackPanel>
</DataTemplate>
Given this Model
public class Family
{
public Family()
{
this.Members = new ObservableCollection<FamilyMember>();
}
public string Name { get; set; }
public ObservableCollection<FamilyMember> Members { get; set; }
}
public class FamilyMember
{
public string Name { get; set; }
public int Age { get; set; }
}
Take a look at this for reference:
TreeView, data binding and multiple templates

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>

How Binding Class inside Class in Windows Phone

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>

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