Bind web xml attributes in listbox - windows-phone-7

I m binding the web xml but its reading only first record
XML
<?xml version="1.0"?>
<content>
<content_row id="1" day="1" title="test" from="01:10" first_name="jitendra" last_name="shakyawar" about_keynote="test" image="1326091608.jpg" innhold="1" about_speaker="test" desc="" flattr_url=""/>
<content_row id="4" day="1" title="test 4" from="04:20" first_name="" last_name="" about_keynote="" image="" innhold="2" about_speaker="" desc="Test 4" flattr_url=""/>
</content>
XAML:
<cc:TabControl HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,80,0,0">
<cc:TabItem Name="tabDag1" Height="50" Width="80" Header="Dag 1" Style="{StaticResource TabItemStyle1}" Foreground="Black" >
<Grid x:Name="ContentGrid" Grid.Row="1" HorizontalAlignment="Center" Margin="5,0,0,0">
<ListBox Name="listDag1" Width="440" Background="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="auto" HorizontalAlignment="Left" Margin="0,20,20,0">
<TextBlock TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Text="{Binding From}" FontWeight="Bold" FontSize="28"/>
<TextBlock TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Text="{Binding FirstName}"/>
<TextBlock TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Text="{Binding LastName}"/>
<TextBlock TextWrapping="Wrap" Style="{StaticResource PhoneTextNormalStyle}" Text="{Binding AboutSpeaker}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</cc:TabItem>
</cc:TabControl>
C#
XDocument xdoc = XDocument.Parse(e.Result);
var data = from query in xdoc.Descendants("content")
select new ContentItems
{
FirstName = (query.Element("content_row") == null) ? "" : (string)query.Element("content_row").Attribute("first_name").Value,
LastName = (query.Element("content_row") == null) ? "" : (string)query.Element("content_row").Attribute("last_name").Value,
From = (query.Element("content_row") == null) ? "" : (string)query.Element("content_row").Attribute("from").Value,
AboutSpeaker = (query.Element("content_row") == null) ? "" : (string)query.Element("content_row").Attribute("about_speaker").Value
};
listDag1.ItemsSource = data;

As pointed out in the comments, you're misunderstanding how to use Linq2xml. You shouldn't include the root element in the query. So your query should look somewhat like this:
var data =
from query in xdoc.Descendants("content_row")
select new ContentItems
{
FirstName = query.Element("content_row").Attribute("first_name").Value,
LastName = query.Element("content_row").Attribute("last_name").Value,
From = query.Element("content_row").Attribute("from").Value,
AboutSpeaker = query.Element("content_row").Attribute("about_speaker").Value
};
Of course, in the case that the attribute is missing, you'll have to check for it manually.

Related

How to bind data in gridview format in windows phone?

Hi i am developing windows phone 8 app.i want to display image on grid view format and i am using listbox inside grid but i didn't get any changes in my output.my code is given below,I want display data on grid view format.
<Grid x:Name="ContentPanel" Margin="0,115,0,0" Background="#424340" Grid.RowSpan="5" />
<StackPanel Margin="0,0,0,0.083" Grid.Row="2" VerticalAlignment="Top" HorizontalAlignment="Center" Grid.RowSpan="2">
<ListBox x:Name="List12" ItemsSource="{Binding}" VerticalAlignment="Top" SelectionChanged="NotchsList12_SelectionChanged"
Margin="0,0,0,0" HorizontalAlignment="left" Width="Auto" Grid.RowSpan="2">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
</StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Titles}" Foreground="Black" Width="189" Height="34" TextWrapping="Wrap" Padding="0,0,0,0"></TextBlock>
<Image Source="{Binding Images}" Width="189" Height="195" Name="value" Stretch="Fill" VerticalAlignment="Top" ></Image>
<TextBlock Text="Text1" Margin="0,0,10,0" HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="1" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
XDocument xmlDoc = XDocument.Parse(dataInXmlFile);
var query = from l in xmlDoc.Descendants("Category")
select new Class
{
Titles = l.Attribute("title").Value,
Images = l.Attribute("image").Value,
Articles = l.Element("SubCategory").Elements("Subcategory")
.Select(article => new Subclass
{
name = article.Attribute("name").Value,
Subimage = article.Attribute("subimage").Value,
Product = article.Element("Product").Elements("product")
.Select(articles => new Product
{
Price = articles.Element("productprice").Value,
ProductName = articles.Attribute("name").Value,
ProductImage = articles.Element("productimage").Value,
Shortdescription = articles.Element("productshortdiscription").Value
}).ToList(),
})
.ToList(),
};
foreach (var result in query)
{
Console.WriteLine(result.Titles);
Console.WriteLine(result.Images);
}
List12.DataContext = query;
I got out put like given below
1.Door 2.window 3.table 4.chair
I need out put like given below image
1.Door 2.window
3.table 4.chair
make ur stackPanel Orientation Vertical instead of Horizontal
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Top">
</StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
Dont use grid, instead use LongListSelector using LayoutMode=Grid
Example:
<phone:LongListSelector ItemsSource="{Binding Categories}" LayoutMode="Grid" GridCellSize="200,200">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Border Background="#e67e22" Height="190" Width="190" Margin="6,0,0,0" Tap="Border_Tap" >
<TextBlock Text="{Binding Name}"></TextBlock>
</Border>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

Bind datetime data to Listbox in window phone 7

I have listbox and I want to bind data to it. I have a datetime field. I have saved my data in datetime field as eg. 01/01/2013 12.00.00. Now When I bind data to listbox it display as I saved but I want to display as only 01/01/2013.
XAML Code:
<Grid x:Name="ContentPanel" >
<ListBox x:Name="listExpense" SelectionChanged="listExpense_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate >
<!--<Button x:Name="btndetails" Width="460" Height="65" BorderThickness="1" Margin="0,-20,0,0" Click="btndetails_click">
<Button.Content>-->
<StackPanel Orientation="Vertical">
<StackPanel.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="White" Offset="0"/>
</LinearGradientBrush>
</StackPanel.Background>
<Border BorderBrush="#120221" Background="Transparent" BorderThickness="6" >
<StackPanel Orientation="Horizontal">
<TextBlock Width="200" Foreground="Black" FontSize="22" Margin="10,0,0,0" Text="{Binding CategoryName}" Height="30"></TextBlock>
<TextBlock Width="70" Foreground="Black" FontSize="22" Margin="0,0,0,0" Text="{Binding Price}" Height="30"></TextBlock>
<TextBlock Width="130" Foreground="Black" FontSize="22" Margin="25,0,50,0" Text="{Binding Date}" Height="30"></TextBlock>
</StackPanel>
</Border>
</StackPanel>
<!--</Button.Content>
</Button>-->
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid
XAMl.cs
var varExp = from Exp in Empdb.Expense
join cat in Empdb.Category
on Exp.CategoryId equals cat.CategoryID
select new { Exp.ExpenseID, Exp.Date, Exp.Price, Exp.Description, cat.Name };
foreach (var item in varExp)
{
string[] formats = { "dd/MM/yyyy" };
ExpenseVO objExpense = new ExpenseVO();
string strDate = item.Date.ToString("dd/MM/yyyy");
objExpense.Date = DateTime.ParseExact(strDate, formats, new CultureInfo("en-US"), DateTimeStyles.None);
objExpense.Price = item.Price;
objExpense.CategoryName = item.Name;
ExpenseList.Add(objExpense);
}
You can format your Text property:
Text="{Binding Date, StringFormat='dd/MM/yyyy'}"

Binding in Nested List Boxes to multiple classes

I'm making a nested listed box , basically because I need to bind multiple classes in a single list box , which I'm not able to do and hence the nested listed box.
Here's what I do in the XAML page :
<ListBox Name="abcd" Margin="10,0,30,0" ItemsSource="{Binding Title}" SelectionChanged="ListBox_SelectionChanged" Height="486" Width="404" FontSize="20">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel Margin="0,0,10,0" Width="380" Height="140">
<Grid >
<TextBlock Text="{Binding cdata}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeLarge}" />
<ListBox Name="ab" ItemsSource="{Binding Description}" FontSize="14">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="380" Height="100">
<Grid>
<TextBlock Text="{Binding cdata}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeLarge}" />
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
where ListBox "abcd" has to be bound with class title and "ab" to the class Description.Both the classes have just one string field , "cdata".
In the xaml.cs I do :
abcd.ItemsSource=from article in root.openfooty.news.article
select new Classes.Title
{
cdata = article.title.cdata
};
ab.ItemSource = from article in root.openfooty.news.article
select new Classes.Description
{
cdata = article.description.cdata
};
binding with "abcd" works fine but with "ab" it says "the nam ab doesnt exist in the current context"
Any help would be much appreciated. Thanks :D
Why don't you write a single class like this
public class TitleDescription
{
public string title { get; set; }
public string description { get; set; }
}
and try the databinding ?
abcd.ItemsSource=from article in root.openfooty.news.article
select new Classes.TitleDescription
{
title = article.title.cdata,
description = article.description.cdata
};
And have only one list box like this
<ListBox Name="abcd" Margin="10,0,30,0" SelectionChanged="ListBox_SelectionChanged" Height="486" Width="404" FontSize="20">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel Margin="0,0,10,0" Width="380" Height="140">
<Grid >
<TextBlock Text="{Binding description}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeLarge}" />
<TextBlock Text="{Binding title}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeLarge}" />
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

Windows Phone 7 MVVM databinding form

I'm new to Windows Phone development, and currently have a simple form. I'm using the MVVM pattern and MvvmLight to run a command. The "Applicant" entity is always empty, and it looks like the 2 way databinding isn't working. Here is my code, I hope someone can point me in the right direction.
View
<Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{Binding Applicant}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="create account" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<StackPanel>
<TextBlock FontSize="15" TextWrapping="Wrap" Margin="0,0,0,10" Text="Please register to create your applicant account. No information will be passed to third parties for any reason."></TextBlock>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="Forename" Width="100"></TextBlock>
<TextBox Width="350" BorderBrush="Red" DataContext="{Binding Forename, Mode=TwoWay}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="Surname" Width="100"></TextBlock>
<TextBox Width="350" x:Name="Surname" BorderBrush="Red" DataContext="{Binding Surname, Mode=TwoWay}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="Email" Width="100"></TextBlock>
<TextBox Width="350" x:Name="EmailAddress" BorderBrush="Red" DataContext="{Binding EmailAddress, Mode=TwoWay}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="Password" Width="100"></TextBlock>
<PasswordBox Width="350" x:Name="PassPhrase" BorderBrush="Red" DataContext="{Binding PassPhrase, Mode=TwoWay}"></PasswordBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="City" Width="100"></TextBlock>
<TextBox Width="350" x:Name="City" DataContext="{Binding City, Mode=TwoWay}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="State" Width="100"></TextBlock>
<TextBox Width="350" x:Name="County" DataContext="{Binding County, Mode=TwoWay}"></TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock VerticalAlignment="Center" Text="Country" Width="100"></TextBlock>
<TextBox Width="350" x:Name="Country" DataContext="{Binding Country, Mode=TwoWay}"></TextBox>
</StackPanel>
<StackPanel>
<Button Name="btnContact"
Content="Register"
DataContext="{Binding ElementName=this, Path=DataContext}"
Command="{Binding SaveApplicantCommand}"
Width="300"/>
</StackPanel>
</StackPanel>
</Grid>
</Grid>
View code behind
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
ApplicantViewModel vm = new ApplicantViewModel();
DataContext = vm;
}
View Model (the view model inherits from ViewModelBase which implements INotifyPropertyChanged)
public class ApplicantViewModel : ViewModelBase
{
public ApplicantViewModel()
{
SaveApplicantCommand = new RelayCommand(SaveApplicant);
Applicant = new Applicant();
}
public ICommand SaveApplicantCommand {get; set;}
void SaveApplicant()
{
if (string.IsNullOrEmpty(Applicant.Forename))
{
MessageBox.Show("Please enter a forename", "Oops...", MessageBoxButton.OK);
return;
}
db.AddObject("Applicants", Applicant);
db.BeginSaveChanges(OnChangesSaved, db);
}
void OnChangesSaved(IAsyncResult result)
{
}
private Applicant _applicant;
public Applicant Applicant
{
get
{
return _applicant;
}
set
{
if (_applicant != value)
{
_applicant = value;
RaisePropertyChanged("Applicant");
}
}
}
}
Whatever is entered into the Forename text box, I always get the error message because Forename is null.
***** UPDATE *********
Here is the Forename property in the model
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
public string Forename
{
get
{
return this._Forename;
}
set
{
this.OnForenameChanging(value);
this._Forename = value;
this.OnForenameChanged();
this.OnPropertyChanged("Forename");
}
}
An ApplicantViewModel does not have a Forename property. Only Applicant has. So do the following:
<TextBox Width="350" BorderBrush="Red"
DataContext="{Binding Applicant.Forename, Mode=TwoWay}"/>
do the same with the other text boxes.

not clearing the listbox items when i opened popup second time in wp7

I have used the list box in the pop up .when i navigate to popup second time the list box items not cleared.how to clear the list box items..please help me on this query.
here is the code i have used
<ListBox Background="LightYellow" ItemsSource="{Binding Mode=OneWay, Path= MyPrintingSheetItems}" Grid.ColumnSpan="2" Grid.Row="6" Height="270" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="ListBox2" VerticalAlignment="Top" Width="440" >
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="0,0,0,3" BorderBrush="pink" >
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="170"/>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="90"/>
<ColumnDefinition Width="90"/>
</Grid.ColumnDefinitions>
<TextBlock Name="lblps" Height="50" Text="{Binding PrintingSheet}" FontSize="26" Grid.Column="0" VerticalAlignment="Center" Margin="0,0, 0, 0" HorizontalAlignment="Center" Foreground="Black" />
<TextBlock Height="50" Name="lblnrml" Text="{Binding Normal}" FontSize="26" Grid.Column="1" Margin="0,0, 0, 0" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Black" />
<TextBlock Height="50" Name="lblhvy" Text="{Binding Heavy}" FontSize="26" Grid.Column="2" Margin="0,0, 0, 0" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Black" />
<TextBlock Height="50" Name="lblmetal" Text="{Binding Metal}" FontSize="26" Grid.Column="3" Margin="0,0, 0, 0" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Black" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
the button click event code :
Private Sub btnAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
PopUp.IsOpen = True
btnAdd.IsEnabled = False
btnBack.IsEnabled = False
btnDelete.Visibility = Windows.Visibility.Collapsed
txtLength.Text = ""
txtwidth.Text = ""
txtsize.Text = ""
CheckBox1.IsChecked = False
ListBox1.IsEnabled = False
TitleLayOut.Opacity = 0.5
LayoutRoot.Background = BrushFromColorName("#AA000000")
txtSearch.IsEnabled = False
btnSearch.IsEnabled = False
ListBox1.Opacity = 0.5
End Sub
when you open the popup make it clear by
listBox1.Items.Clear();

Resources