displaying images from server in a list box - c#

On Windows Phone 8 I have a page that loads user places. Each place consists of name and image name. I then search for the image in the storage and display it. All of that is viewed in a list box using data binding. I do not know how to set the image source in the data binding. Should I make an extra variable of type bit image in the Place class, or in the get of string image name should I call the function which returns a bit image?
Here is the Place class:
class Place
{
public string id { get; set; }
public string user_id { get; set; }
public string name { get; set; }
public string radius { get; set; }
public string longitude { get; set; }
public string latitude { get; set; }
public string image { get; set; }
public string thumbnail { get; set; }
public async Task<placesobj> getUserPlaces(string userId)
{
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string> ("id", userId),
new KeyValuePair<string, string> ("data", "all_places")
};
var serverData = serverConnection.connect("places.php", pairs);
placesobj json = JsonConvert.DeserializeObject<placesobj>(await serverData);
if (json != null)
return json;
else
return null;
}
public class placesobj
{
public List<Place> places { get; set; }
}
}
}
And here is the XAML for the list box:
<ListBox Grid.Row="1" ItemsSource="{Binding places}" Margin="0,10,0,0" >
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel Margin="10,0,10,8">
<StackPanel Orientation="Horizontal" Margin="10,0,10,8">
<TextBlock Text="{Binding name}" TextWrapping="Wrap" FontSize="50" />
<Image Source="{Binding }"
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Try this. I think this will be fine . this is working for me.
reference
Image source is expecting a uri . so better give this as uri
<Image Source="{Binding Image}"/>
public Uri Image { get; set; }

Related

How can I update edited data in WPF using MVVM? [duplicate]

This question already has answers here:
INotifyPropertyChanged WPF
(3 answers)
using of INotifyPropertyChanged
(3 answers)
Closed 4 months ago.
I am building a UI design for chat app and I encountered a problem when trying to update messages of a selected contact.
Uppon selecting a exsisting contact, choosing an edit option and then editing its properties like username and image, the only changes that are made are the username and the image of an contact. I still want to change the Username and image of a MessageModel which is included in my ContactModel as observable collection.
Here is a screenshot of not edited contact:
And a screenhsot of edited contact. You can se that there are only changes being applied on contacs and not in messages cards:
Here is my ContactModel class code:
namespace UV_MessagingApp.MVVM.Model
{
public class ContactModel
{
public string Username { get; set; }
public ImageSource ImageSource { get; set; }
public ObservableCollection<MessageModel> Messages { get; set; }
public string LastMessage => Messages.Last().Message;
}
}
Here is my MessageModel class code:
namespace UV_MessagingApp.MVVM.Model
{
public class MessageModel
{
public string Username { get; set; }
public string UsernameColor { get; set; }
public ImageSource ImageSource { get; set; }
public string Message { get; set; }
public DateTime Time { get; set; }
public bool? FirstMessage { get; set; }
}
}
Here is the binding from "EditContact.xaml". I bind the Username property from ContactModel and I want to achieve that the changes will also be applied to MessageModel properties.
<TextBox Grid.Column="1" Grid.Row="0" Name="contactUsername" Background="Transparent"
Foreground="White" BorderBrush="#292B2F" Margin="2" Padding="2"
Text="{Binding SelectedContact.Username, Mode=TwoWay}"/>
And finally this is the ListView from MainWindow.xaml which is a list where messages are displayed. You can see that I have a "chatItem" where the properties of a MessageModel are binded:
<ListView ItemsSource="{Binding SelectedContact.Messages}"
Background="Transparent"
BorderThickness="0"
ItemContainerStyle="{StaticResource ChatItem}"
Margin="8,0,0,0"
Grid.Row="1">
</ListView>
This is a ChatItem:
<StackPanel Orientation="Horizontal">
<Ellipse Width="30" Height="30"
Margin="10,0,0,-5">
<Ellipse.Fill>
<ImageBrush ImageSource="{Binding ImageSource}"
RenderOptions.BitmapScalingMode="Fant">
</ImageBrush>
</Ellipse.Fill>
</Ellipse>
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Username}"
Foreground="{Binding UsernameColor}"
FontWeight="SemiBold"
VerticalAlignment="Center"
Margin="0,0,-5,0">
</Label>
<Label Content="{Binding Time}"
Foreground="#44474D"
FontWeight="SemiBold"
VerticalAlignment="Center">
</Label>
</StackPanel>
<Label Content="{Binding Message}"
Foreground="White"
FontWeight="SemiBold"
VerticalAlignment="Center">
</Label>
</StackPanel>
</StackPanel>
In the EditContact.caml window I tried passing in the SelectedContact.Messages.Username binding but it doesn't work. So I would appreciate your help.
EDIT:
Here is the MainVievModel class with already imlemented INotifyPropertyChanged() function.
public class MainViewModel : ObservableObject
{
//public ObservableCollection<MessageModel> Messages { get; set; }
public ObservableCollection<ContactModel> Contacts { get; set; }
/* Commands */
public RelayCommand SendCommand { get; set; }
private ContactModel _selectedContact;
public ContactModel SelectedContact
{
get{ return _selectedContact; }
set
{
_selectedContact = value;
OnPropertyChanged();
}
}
private string _message;
public string Message
{
get { return _message; }
set
{
_message = value;
OnPropertyChanged();
}
}
public MainViewModel()
{
//Messages = new ObservableCollection<MessageModel>();
Contacts = new ObservableCollection<ContactModel>();
// Pošiljanje sporočil
//SendCommand = new RelayCommand(o =>
//{
// Messages.Add(new MessageModel
//
}
}

How to send data from a picker IN a collectionview to an API endpoint using MVVM (Xamarin)

For my current project I am working on a cross-platform application. We are building this using Visual studio, cross platform-app.
In this project we use questionaires which we set in an SQL Database.
Using MVVM I can load this dynamically into the mobile app. (We do this because there are many platforms where we show this, so we choose to set it in the back-end, the questionaire with the answers to each question).
But with saving there seems to be an issue,
I can't seem to send data back to my back end
-> I want to send either the value or the string showed from the picker back to the back end (preferably using MVVM). If I can get the data in the viewmodel (or code behind if MVVM would not be possible), i would be helped enough with my question.
I build my questionaire using a collectionview which contains a DataTemplate with a picker inside.
QuestionaireView.xaml:
<ContentPage.BindingContext>
<viewModels:Questionaire_viewmodel/>
</ContentPage.BindingContext>
<CollectionView
x:Name="CollectionQuestionsView"
SelectionMode="None"
Margin="0,0,0,0" RemainingItemsThreshold="5"
BackgroundColor="Transparent"
SelectedItems="{Binding selectedPickers}"
ItemsSource="{Binding Questionaire.Vraag,Mode=TwoWay}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Style="{StaticResource RegularLabelStyleSmallLogin}" Text="{Binding vraagstelling}" HorizontalTextAlignment="Start" VerticalTextAlignment="Start" LineBreakMode="WordWrap"/>
<Picker Style="{StaticResource PickRegularSmall}" SelectedItem="{Binding vraag_id}" x:Name="{Binding vraag_id}" ItemsSource="{Binding Antwoorden}" ItemDisplayBinding="{Binding antwoord_vraagstelling}"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Frame Margin="0,0,0,20" Style="{StaticResource OrangeLoginFrame}">
<Grid Margin="-15" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Style="{StaticResource SemiBoldLabelStyleMedium}" Grid.Row="0" Grid.Column="0" Text="Opslaan"/>
<Button Style="{StaticResource LoginButton}" Command="{Binding SaveVragenlijstCompetentiesCommand}" Grid.Row="0" Grid.Column="0" />
</Grid>
</Frame>
<Button x:Name="questionaireButton" Command="{Binding getQuestionaireLoopbaancompetenties}" IsVisible="False"/>
So to explain the bindings (since some names are not English):
itemsource of collectionview: The questionaire.vraag retrieves a collection of questions out of the questionaire object (see the model.cs down below)
label text binding: Displays the question
picker selectedItem binding: vraag id results in a string (retrieved from the backend) which here specifly is vraag_127, vraag_128 (the numbers are an id of the question)
itemsource picker: binding antwoorden, these are the answers to the questions
in the frame below the collectionview, I made a button to (at least i tried) to send the data to the ViewModel.cs
The (hidden)button below the frame loads the data (I execute it in the code behind, not sure if this is the best option)
QuestionaireViewModel.cs
public string vraag_127 { get; set; }
public string vraag_128 { get; set; }
... and so on
public string vraag_161 { get; set; }
public string categorie_vraag { get; set; }
public string SelectedAnswer { get; set; }
public string vraagstelling { get; set; }
public string vraag_id { get; set; }
public string score_antwoord { get; set; }
public string antwoord_vraagstelling { get; set; }
public string default_question { get; set; }
public string description { get; set; }
private Questionaire_model _questionaire;
public Questionaire_model Questionaire
{
get { return _questionaire; }
set
{
_questionaire = value;
OnPropertyChanged();
}
}
private List<Questionaire_model> _vraag;
public List<Questionaire_model> Vraag
{
get { return _vraag; }
set
{
_vraag = value;
OnPropertyChanged();
}
}
private List<Questionaire_model> _antwoorden;
public List<Questionaire_model> Antwoorden
{
get { return _antwoorden; }
set
{
_antwoorden = value;
OnPropertyChanged();
}
}
public ICommand getQuestionaireLoopbaancompetenties
{
get
{
return new Command(async (boolean) =>
{
Questionaire = await _apiServices.GetQuestionaireQuestions("Competenties");
});
}
}
private ObservableCollection<vragenModel> _selectedPicker;
public ObservableCollection<vragenModel> SelectedPicker
{
get
{
return _selectedPicker;
}
set
{
_selectedPicker = value;
OnPropertyChanged();
}
}
public ICommand SaveVragenlijstCompetentiesCommand
{
get
{
return new Command(async () =>
{
var responseBool = await _apiServices.SaveCultuur(vraag_127,vraag_128, ... vraag_161)});
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyname = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
And my QuestionaireModel.cs:
public class Questionaire_model
{
public int id { get; set; }
public string default_question { get; set; }
public string description { get; set; }
public IList<Vragen> Vraag{ get; set; }
public class Vragen
{
public string categorie_vraag { get; set; }
public string SelectedAnswer { get; set; }
public string vraagstelling { get; set; }
public string vraag_id { get; set; }
public IList<antwoorden> Antwoorden { get; set; }
}
public class antwoorden
{
public string score_antwoord { get; set; }
public string antwoord_vraagstelling { get; set; }
}
}
The reason we use an collectionView, is because we use the template for various questionaires with various amount of questions and answer possibilities.
So at this moment, when I execute my save action, it gives "null" in my ViewModel - action
I have defined public class Questionaire_viewmodel : INotifyPropertyChanged above my viewmodel
So I hope someone can either help me with my code, that I missed something here
Or if I have to change something because it is simply impossible well then I would like to have an direction where to go to.
As I understand, you want your Picker.SelectedItem actually bind to QuestionaireViewModel.vrag_xxx properties. That won't work like this - what now happens is that your Picker.SelectedItem is bound to Vragen.vraag_id field and is not able to use it, as long as it expects antwoorden type there.
However, I see that you have SelectedAnswer property in Vragen, you can change it to be of antwoorden type and then fetch user choice from that field.
So, if your Vragen class is:
public class Vragen
{
public string categorie_vraag { get; set; }
public antwoorden SelectedAnswer { get; set; }
public string vraagstelling { get; set; }
public string vraag_id { get; set; }
public IList<antwoorden> Antwoorden { get; set; }
}
Then, you can change your binding for Picker to:
<Picker Style="{StaticResource PickRegularSmall}" SelectedItem="{Binding SelectedAnswer}" x:Name="{Binding vraag_id}" ItemsSource="{Binding Antwoorden}" ItemDisplayBinding="{Binding antwoord_vraagstelling}"/>
And, at last in SaveVragenlijstCompetentiesCommand you'll be able to get array of answers (sorted by question id):
Questionaire.Vraag.OrderBy(x => x.vraag_id).Select(x => x.SelectedAnswer.antwoord_vraagstelling)
That will give you array of answer texts. Still it doesn't conform to your apiServices.SaveCultuur call. I would strongly recommend to change that method - method that contains a lot of parameters is certainly a problem.
If you can change it to accept string[], then you will be able to pass Questionaire.Vraag.OrderBy(x => x.vraag_id).Select(x => x.SelectedAnswer.antwoord_vraagstelling) to it.
If you can't change apiServices, there's still possibility to call the method through reflection using array, but it's a bit tricky.

c# deserialize json array and data binding

I have trouble to deserialize this json file. I want to bind the "favoriteSongs" array to my view in xaml and display all the informations in a longlistselector.
{
"_id": "56fd864ff28510694a6a7b5e",
"addFavorites": {
"favoriteSongs": [
{
"_id": "570aeb89869a0520e0d0365c",
"title": "get lucky",
"album": "random access memory",
"primaryArtist": "daft punk",
"artist": "570ae933cd7d1072b430ea51",
"primaryGenre": "electro",
"genre": "570ae933cd7d1072b430ea52",
"duration": "3m31s",
"year": 2013
},
{ ... }
]
}
In my view model im using an observablecollection to add the informations from the json, here the "getFavoriteSongsModel" is my RootObject
for (int i = 0; i < count; i++)
{
getFavoriteSongsModel = JsonConvert.DeserializeObject<GetFavoriteSongsModel>(e.Result);
getFavoriteSongs.Add(new GetFavoriteSongsModel()
{
Id = getFavoriteSongsModel.Id,
Favorites = getFavoriteSongsModel.Favorites
});
I tried to use this pattern with another observablecollection that points to a class with a list of all the infomations i need but it crashes everytime. I don't know how to use the "Favorites" variable, i know it is populated but i can't display what i want in the xaml.
<Grid>
<phone:LongListSelector x:Name="MainLongListSelector" Margin="0,0,-12,0" ItemsSource="{Binding getFavoriteSongs}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" >
<TextBlock Text="{Binding FavoriteSongs.Album}" Style="{StaticResource PhoneTextBlockBase}"/>
<TextBlock Text="{Binding FavoriteSongs.Title}" Style="{StaticResource PhoneTextBlockBase}"/>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
I have create a sample Code on base of my understanding.
I have Created three classes on based of Json string from json2csharp, which is very useful to create classes on based of Json string.
Three classes are as following
public class FavoriteSong
{
public string _id { get; set; }
public string title { get; set; }
public string album { get; set; }
public string primaryArtist { get; set; }
public string artist { get; set; }
public string primaryGenre { get; set; }
public string genre { get; set; }
public string duration { get; set; }
public int year { get; set; }
}
public class AddFavorites
{
public ObservableCollection<FavoriteSong> favoriteSongs { get; set; }
}
public class MainClass
{
public string _id { get; set; }
public AddFavorites addFavorites { get; set; }
}
I have just use your string as json and on base I have create this samle.
Following code will deserialize json string
public MainClass objMainClass { get; set; }
string strJson = "{\"_id\": \"56fd864ff28510694a6a7b5e\",\"addFavorites\": {\"favoriteSongs\": [{\"_id\": \"570aeb89869a0520e0d0365c\",\"title\": \"get lucky\",\"album\": \"random access memory\",\"primaryArtist\": \"daft punk\",\"artist\": \"570ae933cd7d1072b430ea51\",\"primaryGenre\": \"electro\",\"genre\": \"570ae933cd7d1072b430ea52\",\"duration\": \"3m31s\",\"year\": 2013}]}}";
objMainClass = new MainClass();
objMainClass = JsonConvert.DeserializeObject<MainClass>(strJson);
MainLongListSelector.DataContext = objMainClass.addFavorites;
and binding in XAML is as following
<phone:LongListSelector
x:Name="MainLongListSelector"
Margin="0,0,-12,0"
ItemsSource="{Binding favoriteSongs}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel
Margin="0,0,0,17">
<TextBlock
Text="{Binding album}"
Style="{StaticResource PhoneTextBlockBase}" />
<TextBlock
Text="{Binding title}"
Style="{StaticResource PhoneTextBlockBase}" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
Hope so it will be helpful to you.

Windows phone 8.1 binding data to XAML from datasource (HUB template)

I need help...
So essentially I've been watching WP 8.1 development tutorials and there was a tutorial with the standard HUB template.
So I tried to configure this template for my own use. I have a JSON file with all the data I need (apartments) which I want list now on my page. I "recreated" the SampleDataSource which is in my app just DataSource. I think everything is OK with the data and now comes the problem with translating this data and binding it to XAML. I don't really know why it isn't working as I have just tried to change the paramters which work in the template (provided by the SDK). Could you guys help me or give me some directions where can I find example for doing this.
If someone would be willing to help or skype/TW with me just to show me the structure and what I have to do, I would be grateful as I would then know how this basically translates and works so I could work on other sections of the project! Have bitcoins as a reward! :)
Thx in advance.
EDIT: Add Github repo: https://github.com/lklancir/zimmerfrei_wp/tree/master/ZimmerFrei_v0.1
Here is the DataSource.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Json;
using Windows.Storage;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
namespace ZimmerFrei.Data
{
public class ApartmentData
{
public ApartmentData(String id, String name, String description, String capacity, String stars, String address, String email, String phone, String phone2, String rating, String lat, String lng, String price, String cover_photo, String owner_id, String type_id, String city_id)
{
this.Id = id;
this.Name = name;
this.Description = description;
this.Capacity = capacity;
this.Stars = stars;
this.Address = address;
this.Email = email;
this.Phone = phone;
this.Phone2 = phone2;
this.Rating = rating;
this.Lat = lat;
this.Lng = lng;
this.Price = price;
this.Cover_photo = cover_photo;
this.Owner_id = owner_id;
this.Type_id = type_id;
this.City_id = city_id;
}
public string Id { get; private set; }
public string Name { get; private set; }
public string Description { get; private set; }
public string Capacity { get; private set; }
public string Stars { get; private set; }
public string Address { get; private set; }
public string Email { get; private set; }
public string Phone { get; private set; }
public string Phone2 { get; private set; }
public string Rating { get; private set; }
public string Lat { get; private set; }
public string Lng { get; private set; }
public string Price { get; private set; }
public string Cover_photo { get; private set; }
public string Owner_id { get; private set; }
public string Type_id { get; private set; }
public string City_id { get; private set; }
public override string ToString()
{
return this.Name;
}
}
public sealed class DataSource
{
private static DataSource _dataSource = new DataSource();
private ObservableCollection<ApartmentData> _apartments = new ObservableCollection<ApartmentData>();
public ObservableCollection<ApartmentData> Apartments
{
get { return this._apartments; }
}
public static async Task<IEnumerable<ApartmentData>> GetApartmentsAsync()
{
await _dataSource.GetDataAsync();
return _dataSource.Apartments;
}
public static async Task<ApartmentData> GetApartmentAsync(string id)
{
await _dataSource.GetDataAsync();
var matches = _dataSource.Apartments.Where((apartment) => apartment.Id.Equals(id));
if (matches.Count() == 1) return matches.First();
return null;
}
private async Task GetDataAsync()
{
if (this._apartments.Count != 0)
return;
Uri dataUri = new Uri("ms-appx:///DataModel/Apartments.json");
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
string jsonText = await FileIO.ReadTextAsync(file);
JsonObject jsonObject = JsonObject.Parse(jsonText);
JsonArray jsonArray = jsonObject["apartments"].GetArray();
foreach (JsonValue apartmentValue in jsonArray)
{
JsonObject apartmentObject = apartmentValue.GetObject();
ApartmentData apartment = new ApartmentData(apartmentObject["Id"].GetString(),
apartmentObject["Name"].GetString(),
apartmentObject["Description"].GetString(),
apartmentObject["Capacity"].GetString(),
apartmentObject["Stars"].GetString(),
apartmentObject["Address"].GetString(),
apartmentObject["Email"].GetString(),
apartmentObject["Phone"].GetString(),
apartmentObject["Phone2"].GetString(),
apartmentObject["Rating"].GetString(),
apartmentObject["Lat"].GetString(),
apartmentObject["Lng"].GetString(),
apartmentObject["Price"].GetString(),
apartmentObject["Cover_photo"].GetString(),
apartmentObject["Owner_id"].GetString(),
apartmentObject["Type_id"].GetString(),
apartmentObject["City_id"].GetString());
this.Apartments.Add(apartment);
}
}
}
}
Here is the ListPage.xaml (where I want to write out my data)
<Page
x:Class="ZimmerFrei_v0._1.ListPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ZimmerFrei_v0._1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data="using:ZimmerFrei.Data"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
d:DataContext="{Binding Source={d:DesignData Source=/DataModel/Apartments.json, Type=data:DataSource}}"
mc:Ignorable="d" FontFamily="Global User Interface">
<Page.Resources>
<DataTemplate x:Key="StandardTripleLineItemTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" Margin="0,9.5,0,0" Grid.Column="0" HorizontalAlignment="Left">
<Image Source="{Binding Cover_photo}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}" Height="79" Width="79"/>
</Border>
<StackPanel Grid.Column="1" Margin="14.5,0,0,0">
<TextBlock Text="{Binding Name}" Style="{ThemeResource ListViewItemTextBlockStyle}" FontFamily="Global User Interface"/>
<TextBlock Text="{Binding Description}" Style="{ThemeResource ListViewItemContentTextBlockStyle}" Foreground="{ThemeResource PhoneMidBrush}" FontFamily="Global User Interface" />
<TextBlock Text="{Binding Stars}" Style="{ThemeResource ListViewItemSubheaderTextBlockStyle}" />
</StackPanel>
</Grid>
</DataTemplate>
</Page.Resources>
<Grid x:Name="LayoutRoot">
<Hub x:Name="Hub" x:Uid="Hub" Header="ZimmerFrei">
<HubSection x:Uid="HubSection" Header="APARTMENTS"
DataContext="{Binding Apartments}" >
<DataTemplate>
<ListView
AutomationProperties.AutomationId="ItemListViewSection3"
AutomationProperties.Name="Items In Group"
SelectionMode="None"
IsItemClickEnabled="True"
ItemsSource="{Binding apartment}"
ItemTemplate="{StaticResource StandardTripleLineItemTemplate}"
ItemClick="ItemView_ItemClick"
ContinuumNavigationTransitionInfo.ExitElementContainer="True">
</ListView>
</DataTemplate>
</HubSection>
</Hub>
</Grid>
There are a few issues here:
Your ApartmentData class has some properties named like this Cover_photo. This isn't typically the style convention used by C#, so I renamed them to like CoverPhoto.
On the same topic, your ApartmentData properties are named like Cover_photo but they are named like cover_photo in the JSON (which is a typical JSON style). You're accessing the JSON keys like this:
apartmentObject["Cover_photo"].GetString() // KeyNotFoundException
but it should be:
apartmentObject["cover_photo"].GetString()
This will work during runtime, but the designer won't execute this code. The designer reads the JSON file and attempts to match keys in the JSON file to properties on the type you have specified, but it won't be able to because the keys and properties don't match. So either you:
Modify the JSON so that the keys match the properties like CoverPhoto.
Annotate your type with the DataContractAttribute. This allows you to specify the mapping between JSON and .NET type with DataMemberAttribute, but mostly because you get JSON serialization/deserialization!
I sent you a pull request with these changes. Also, your design data is huge (~1000 items!). It should only contain ~5 items since you won't be able to see all items in the designer anyway.

Listbox in other Listbox, Itemsource, Binding

I would like to know How to refresh our ListBox item.
I tried OnPropertyChanged method, ObservableCollection, but it didn't work. I tried set again the itemsource property, so that worked, but now I have got 2 ListBox and now it's complicated. It's a wp7 project there is the main interface. You can see I have 2 listbox
<ListBox Name="lsbNameDays" ItemsSource="ComplexNameDays">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding NameDay.Name}" FontSize="50"/>
<ListBox ItemsSource="ComplexNameDays.FacebookFriends" x:Name="asdf">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Lastname}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
there is the properties:
List<SelectedNameDays> complexNameDays;
public List<SelectedNameDays> ComplexNameDays
{
get { return complexNameDays; }
set
{
complexNameDays = value;
OnPropertyChanged("ComplexNameDays");
}
}
public class SelectedNameDays : Notifier
{
NameDay _nameday;
public NameDay NameDay
{
get { return _nameday; }
set { _nameday = value; OnPropertyChanged("NameDay"); }
}
public List<FacebookFriend> FacebookFriends { get; set; }
public SelectedNameDays()
{
_nameday = new NameDay();
}
}
public class FacebookFriend
{
public long Id { get; set; }
public string Name { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Birthday { get; set; }
public string Gender { get; set; }
public Uri Picture { get; set; }
}
The begining of the code is correct, that works, because when the Constructor set the datas I set retry the itemsource of lbsNameDays, but i cant find the "asdf" listbox, i can't set their datas again.
so the 2 main question are that.
1. how can i fire the property changed if that, and the observable collecton doesn't works.
2. how can I use asdf listbox in the datatemplate
thank the answer, and I sorry my grammer mistakes
Your Bindings won't work, because you dont use the right syntax:
ItemsSource="ComplexNameDays"
should be
ItemsSource="{Binding ComplexNameDays}"
The second binding is also wrong:
ItemsSource="ComplexNameDays.FacebookFriends"

Categories