Listpicker binding issue WP7 - windows-phone-7

I am having an issue when trying to bind an object I created to a listpicker. I have had success using a listpicker with strings and ints but I am running into issues when trying to use my own class/object.
Here is the XML (I have two listpickers, one works and one doesn't)
<toolkit:ListPicker
x:Name="CountryListPicker"
Margin="0,2,0,10" Width="458"
BorderThickness="3" FullModeHeader="Country"
CacheMode="BitmapCache">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Background="LightBlue" Width="34" Height="34">
<TextBlock Text="{Binding _code}" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<TextBlock Text="{Binding _name}" Margin="12 0 0 0"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0 21 0 20">
<TextBlock Text="{Binding _name}" Margin="16 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
<toolkit:ListPicker
x:Name="testPicker"
Header="Accent color"
FullModeHeader="ACCENTS"
CacheMode="BitmapCache">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" Margin="12 0 0 0"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0 21 0 20">
<TextBlock Text="{Binding}"
Margin="16 0 0 0"
FontSize="43"
FontFamily="{StaticResource PhoneFontFamilyLight}"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>`
The first list picker is my new object binding one and the second is binding a string.
Here is the code behind:
ObservableCollection<Country> countries = new ObservableCollection<Country>();
countries.Add(new Country { _code = "US", _name = "United States1"});
countries.Add(new Country { _code = "US", _name = "United States2" });
countries.Add(new Country { _code = "US", _name = "United States3" });
countries.Add(new Country { _code = "US", _name = "United States4" });
countries.Add(new Country { _code = "US", _name = "United States5" });
countries.Add(new Country { _code = "US", _name = "United States6" });
this.CountryListPicker.ItemsSource = new ReadOnlyCollection<Country>(countries);
ObservableCollection<string> _accentColors = new ObservableCollection<string>();
_accentColors.Add("Blue");
_accentColors.Add("Blue2");
_accentColors.Add("Blue3");
_accentColors.Add("Blue4");
_accentColors.Add("Blue5");
_accentColors.Add("Blue6");
_accentColors.Add("Blue7");
this.testPicker.ItemsSource = new ReadOnlyCollection<string>(_accentColors);
The 2nd listpicker is fine and I think this is because it contains strings.
My broken listpicker displays all of the correct data but when I select a listpickeritem, it doesn't select it. The app loads the full listpicker and I click one of the items and when I return back to the main view the selected item isn't reflected in the listpicker.
Anyone have any ideas? Need me to explain anymore?

I figured this one out on my own. I had a Loaded function where the listpicker was initializing. Looks like Loaded gets "loaded" when the listpicker returns from the fullscreen view which in turn resets the selectedIndex. Fix was easy, just move the creation of the lists as well as setting the itemSource from the Loaded function into the constructor.

Related

Get Value for selected Item in ListPicker

I have a listpicker which showing item names. When selecting a item I want to get the Item Id for that selected Item.How can I do That?
<toolkit:ListPicker x:Name="lstItem"
FullModeHeader="Item"
SelectionMode="Single"
ItemsSource="{Binding getItems}"
ExpansionMode="FullscreenOnly"
Margin="12,5,10,0"
Grid.Row="0"
Grid.Column="1"
Background="White"
FontSize="21"
Header=""
Foreground="Black"
BorderThickness="0" Grid.ColumnSpan="2" SelectionChanged="lstItem_SelectionChanged"
>
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="16 15 0 15">
<TextBlock Text="{Binding Name}"
Margin="0 0 0 0"
FontSize="25"
FontFamily="{StaticResource PhoneFontFamilyLight}"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
In C# Code I'm binding both to listpicker.This data directly I'm binding to list picker.
public ObservableCollection<Items> bindItems()
{
listItems.Clear();
//int i = Common.intCustomerId; //&& btypes.ChqRtnPaymentNo == ""
var itemlist = from DailyItemStock DailyItm in APPCommon.SFADB
join ItemMaster IM in APPCommon.SFADB on DailyItm.ItemMasterID equals IM.ID
where DailyItm.StockDate == System.DateTime.Today
select new
{
DailyItm.ItemMasterID,
DailyItm.BatchNo,
IM.Name
};
listItems.Add(new Items() { ItemMasterID = 0, Name = "Select One" });
foreach (var lists in itemlist)
{
listItems.Add(new Items()
{
ItemMasterID = lists.ItemMasterID,
BatchNo = lists.BatchNo,
Name = lists.Name,
});
}
itemlist = null;
return listItems;
}
One possible way :
Items selectedItem = (Items)lstItem.SelectedItem;
int id = selectedItem.ItemMasterID;
in order to get the id number you have to create it in the database.
example
SHOP_ID ='4987bc1b-c0a8-6cb7-12f4-0243011f7099' and (debitor_type is null or debitor_type in (CASE WHEN (select techfund_debitor_enabled from impl_shop where shop_id='4987bc1b-c0a8-6cb7-12f4-0243011f7099')

Windows Phone Toolkit How to get full Popup ListPicker?

I am wondering how do you make it so the list picker goes to a separate screen when clicked on?
I tried
<toolkit:ListPicker Margin="155,109,179,0" VerticalAlignment="Top" ItemTemplate="{StaticResource PriceTypesTemplate1}" ItemsSource="{Binding PriceTypes}" FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}"/>
But that crashes the app saying it can't find PickerFullModeItemTemplate
Did you provide a ressource named PickerFullModeItemTemplate ?
Your code specify two separate custom templates for each of the states (ItemTemplate and FullModeItemTemplate). Here is a very basic example.
c#
public class Cities
{
public string Name { get; set; }
public string Country { get; set; }
public string Language { get; set; }
}
List<Cities> source = new List<Cities>();
source.Add(new Cities() { Name = "Madrid", Country = "ES", Language = "Spanish" });
source.Add(new Cities() { Name = "Las Vegas", Country = "US", Language = "English" });
source.Add(new Cities() { Name = "London", Country = "UK", Language = "English" });
source.Add(new Cities() { Name = "Mexico", Country = "MX", Language = "Spanish" });
this.listPicker.ItemsSource = source;
xaml
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.Resources>
<DataTemplate x:Name="PickerItemTemplate">
<StackPanel Orientation="Horizontal">
<Border Background="LightGreen" Width="34" Height="34">
<TextBlock Text="{Binding Country}" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<TextBlock Text="{Binding Name}" Margin="12 0 0 0"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Name="PickerFullModeItemTemplate">
<StackPanel Orientation="Horizontal" Margin="16 21 0 20">
<TextBlock Text="{Binding Name}" Margin="16 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
<TextBlock Text="language: "/>
<TextBlock Text="{Binding Language}" Foreground="Green"/>
</StackPanel>
</DataTemplate>
</Grid.Resources>
<toolkit:ListPicker ExpansionMode="FullScreenOnly" x:Name="listPicker" ItemTemplate="{StaticResource PickerItemTemplate}" FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}" Header="Cities" FullModeHeader="Cities" CacheMode="BitmapCache"/>
</Grid>
If you do not want to have custom templates then you can use a simple list of strings with the default ones
<toolkit:ListPicker ExpansionMode="FullScreenOnly" x:Name="listPicker" Header="Header" FullModeHeader="Full mode Header" CacheMode="BitmapCache"/>

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'}"

Change fontSize of Item of Listpicker in window phone 7

I have two ListPicker. I have bound lpkfamilymemberfrom database and lpkpaymentmode from array value.I want to two as FullScreen mode and in full screen I want item font size 40. In
lpkfamilymemberfrom I achieve font size 40 as I have done following code.But I dont know how to achive this for lpkpaymentmode becoze it has not any datatemplate becoz I bound it using Array.
<DataTemplate x:Name="PickerFullModeItemTemplate">
<StackPanel Orientation="Horizontal" Margin="16 21 0 20">
<TextBlock Text="{Binding Name}" Margin="16 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
</StackPanel>
</DataTemplate>
<toolkit:ListPicker Background="White" FontSize="44" ExpansionMode="FullScreenOnly" x:Name="lpkpaymentmode"/>
<toolkit:ListPicker ItemTemplate="{StaticResource PickerItemTemplate}" Background="White" FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}" ExpansionMode="FullScreenOnly" x:Name="lpkfamilymember"/>
And Binding Code:
String[] Mode = { "Cash", "Credit Card", "Debit Card","Net Banking" };
InitCustomPickerDialog();
this.lpkpaymentmode.ItemsSource = Mode;
this.lpkfamilymember.ItemsSource = GetfamilyList();
public IList<FamilyVO> GetfamilyList()
{
// Fetching data from local database
IList<FamilyVO> FamilyList = null;
using (ExpenseDataContext Empdb = new ExpenseDataContext(strConnectionString))
{
IQueryable<FamilyVO> ExpQuery = from Exp in Empdb.Family select Exp;
FamilyList = ExpQuery.ToList();
}
return FamilyList;
}
I have tried many code to set item size of lpkpaymentmode to 40 in full mode but every time it show some fix size which did not change.
How about this:
<DataTemplate x:Name="ItemTemplateForPayment">
<TextBlock Text="{Binding }" Margin="16 0 0 0" FontSize="40" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
</DataTemplate>
And then applying like this:
<toolkit:ListPicker ItemTemplate="{StaticResource PickerItemTemplate}" Background="White" FullModeItemTemplate="{StaticResource ItemTemplateForPayment}" ExpansionMode="FullScreenOnly" x:Name="lpkpaymentmode"/>

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>

Resources