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'}"
Related
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>
I have an ObservableCollection<testItem> and every testItem has another ObvervableCollection<testData>.
Know for every testData I want a ListItem with some Data of the testItem and from testData. At the moment I set the ObservableCollection<testItem> as ItemsSource of the List but than I get only the first item of the itemsData.
What can I do?
UPDATE:
xaml:
<ListBox Name="ResultList" ItemsSource="{Binding TankstellenItem}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="#FF3AACDF" BorderThickness="4">
<StackPanel Orientation="Horizontal"
Background="White"
UseLayoutRounding="True" Tap="ItemIsClicked">
<StackPanel Width="130" Orientation="Vertical" Margin="0,0,0,0">
<Border BorderThickness="2"
Background="#FF3AACDF" BorderBrush="White">
<TextBlock Text="{Binding PriceBigDigits}"
FontFamily="Assets/Font.ttf#LCD"
FontSize="25" TextAlignment="Center" />
</Border>
<TextBlock Text="{Binding FuelType}"
TextAlignment="Center"
FontSize="15" Foreground="Black"/>
<TextBlock Text="{Binding UpdateDate}"
TextAlignment="Center"
FontSize="15" Foreground="Black" />
<TextBlock Text="{Binding DistanceString}"
TextAlignment="Center"
FontSize="15" Foreground="Black"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="10,0,0,0" Width="400">
<TextBlock Text="{Binding Title}"
FontSize="30" FontWeight="Bold" Foreground="Black"/>
<TextBlock Text="{Binding Address}" FontSize="20" Foreground="Black" />
<TextBlock Text="{Binding PLZCity}" FontSize="20" Foreground="Black" />
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
than I have the testItem:
public testItem{
DistanceString (string),
Title (string),
Addresse (string),
PLZCity (string),
itemDates (ObservableCollection<itemData>)}
And the itemData:
public itemData{
PriceBigDigits (string),
FuelType (string),
UpdateDate (string)
If I understand you correctly, you may want to bind the itemDates to a ItemsControl's ItemsSource property. So wrap your second StackPanel within the DataTemplate of an ItemsControl.
I have one listbox control in my xaml par.My code for listbox is
<ScrollViewer Grid.Row="2" Name="ScrollGrid" VerticalScrollBarVisibility="Auto" VerticalAlignment="Top" Height="Auto" Width="450" Margin="0,100,0,0" >
<ListBox x:Name="TransactionList" Grid.Row="2" HorizontalAlignment="Center" Margin="0,0,0,0" Width="400" Height="Auto">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0,-10,0,0" Height="120" Width="400" MouseLeftButtonUp="StackPanel_MouseLeftButtonUp">
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Height="80" Width="300" Margin="0,0,20,0">
<TextBlock Height="Auto" Margin="20,0,0,0" VerticalAlignment="Center" Text="vodafone vas" FontSize="30" Foreground="Gray" Name="tbCitynameFavorite" />
</StackPanel>
<StackPanel Orientation="Vertical" Height="60" Width="60" Margin="0,0,0,30">
<Image Name="imgfevdlt" Width="50" Height="40" VerticalAlignment="Center" FlowDirection="LeftToRight" Source="/VodafoneAugmentedReality;component/Images/ArrowMoreSmall.png" />
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="10" Width="350" Margin="-360,60,0,0">
<Image Source="/VodafoneAugmentedReality;component/Images/SaperaterLine.png" Width="350" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
When i run application this will not display any data
But when i comment out this two tag than it will run perfectly
<ListBox.ItemTemplate>
<DataTemplate>
Here i have only static value so it will ok but when i have multiple value than it will create problem
So plase help me i can solve this problem?
You will have to set up a class (say ListBoxItem ) which contains all the properties of your DataTemplate members values
And then define a List of ListBoxItem 's and set it as the ItemSource of your ListBox
Example code
public class ListBoxItem
{
public string CityNameFavorite { set; get; }
// Other required properties follows here
}
List<ListBoxItem> listBoxItems = new List<ListBoxItem>();
listBoxItems.Add(new ListBoxItem() { CityNameFavorite = "Vodafone vas" });
//Add as many items you need
TransactionList.ItemsSource = listBoxItems;
This is just an overview of how it can be achieved, improvise based on your needs
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.
my problem is how can i set listbox which has itemtemplate ,and it also itemteplate of pivot
.i don't have any idea to doing this but it is a need of my project .one another is how to set pivot header . i m trying to do something like that
<controls:Pivot Height="779" Name="m" >
<controls:Pivot.Background>
<ImageBrush ImageSource="/WindowsPhoneApplication7;component
/Images/S.jpeg" />
</controls:Pivot.Background>
<!--<controls:PivotItem Name="all" >-->
<controls:Pivot.ItemTemplate>
<DataTemplate>
<ListBox Height="450" HorizontalAlignment="Stretch" Margin="8,6,0,0" Name="listBox1" VerticalAlignment="Stretch" Background="#00537393" Width="445" >
<ListBox.ItemTemplate >
<DataTemplate >
<Border BorderBrush="Black" CornerRadius="8" BorderThickness="1" HorizontalAlignment="Stretch" Name="border">
<Grid HorizontalAlignment="Stretch" Name="grid1" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="0*" />
<RowDefinition Height="100*" />
</Grid.RowDefinitions>
<Image Height="78" HorizontalAlignment="Left" Margin="13,12,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="92" Grid.Row="1" Source="{Binding ImageSource}" />
<TextBlock Foreground="White" Height="80" HorizontalAlignment="Left" Margin="111,12,0,0" Name="textBlock1" Text="{Binding Title}" VerticalAlignment="Top" Width="280" TextWrapping="Wrap" Grid.Row="1" />
<Image Grid.Row="1" Height="75" HorizontalAlignment="Left" Margin="380,12,0,0" Name="image2" Stretch="Fill" VerticalAlignment="Top" Width="73" Source="/WindowsPhoneApplication7;component/Images/appbar.transport.play.rest.png" />
<TextBlock Foreground="White" Grid.Row="1" Height="20" HorizontalAlignment="Left" Margin="111,88,0,0" Name="textBlock2" Text="{Binding date}" VerticalAlignment="Top" Width="190" FontSize="11" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</controls:Pivot.ItemTemplate>
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="hiii" TextWrapping="Wrap" FontSize="0" Foreground="White" />
</DataTemplate>
</controls:Pivot.HeaderTemplate>
</controls:Pivot>
You can set the header of a Pivot control to pretty much anything since it is an object. To set it to an image instead of a default title set the Title to "" and do this in the constructor of your page:
YourPivotControl.Title = new Image { Width = 400, Source = new BitmapImage(new Uri("/someImage.png", UriKind.Relative)) };
Did you take a look at the default pivot page setup by visual studio? They do exactly what you're looking for:
<!--Pivot Control-->
<controls:Pivot Title="MY APPLICATION">
<!--Pivot item one-->
<controls:PivotItem Header="first">
<!--Double line list with text wrapping-->
<ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PivotItem>