Get Value for selected Item in ListPicker - windows-phone-7

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')

Related

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"/>

Linq - Set a limit to a group while doing a request

I'm trying to find an efficient way to set a limit to each of my group in a linq request in C# (7 items by group for example). I don't want to create another group with the extra of one group I just want to pass to another category.
For the moment I'm doing this to fill my groupedList :
public IEnumerable<object> ListByCategory
{
get
{
var query = from item in listArticles.listArticles
orderby item.categorie
group item by item.categorie into g
select g;
return query;
}
}
I tried to go through this groupedList afterward and remove all the extra element in each category but it is not elegant at all.
Thank you in advance
Here is the Xaml part :
<local:MyGridView x:Name="PicturesGridView" SelectionMode="None"
ItemsSource="{Binding Source={StaticResource cvs1}}" IsItemClickEnabled="True" ItemTemplate="{StaticResource CustomTileItem}" ItemClick="ItemView_ItemClick" >
<local:MyGridView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</local:MyGridView.ItemsPanel>
<local:MyGridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Button Click="Button_Click_1" Content="{Binding Key}" Foreground="Black" Background="White" FontSize="30" Margin="0,0,0,-10" ></Button>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid ItemWidth="75" ItemHeight="150" Orientation="Vertical" Margin="0,0,80,0" MaximumRowsOrColumns="3"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</local:MyGridView.GroupStyle>
</local:MyGridView>
Here are the ressources :
<DataTemplate x:Key="CustomTileItem">
<Grid >
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
<Image Source="{Binding imageUrl}" Stretch="UniformToFill"/>
</Border>
<StackPanel VerticalAlignment="Bottom" >
<TextBlock Text="{Binding title}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="30" Margin="15,0,15,0"/>
<TextBlock Text="{Binding chapo}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
</StackPanel>
</Grid>
</DataTemplate>
<CollectionViewSource x:Name="cvs1"
IsSourceGrouped="True" />
and I'm doing the binding like this :
IEnumerable<object> myObject = App.api.ListByCategory;
this.cvs1.Source = App.api.ListByCategory;
If I understand your request correctly, you can just use Take:
return from item in listArticles.listArticles
orderby item.categorie
group item by item.categorie into g
select g.Take(7);
(Obviously that can be a variable...)
Note that this will lose the fact that it's a grouping, so you'll no longer be able to take the key from each group. If that's a problem, you can select to a new data structure easily enough. For example:
return from item in listArticles.listArticles
orderby item.categorie
group item by item.categorie into g
select new { g.Key, Values = g.Take(7) };

Getting custom data from ListPicker

I have a ListPicker in my app which is defined like that:
<DataTemplate x:Name="PickerItemTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding code}" Margin="12 0 0 0" Visibility="Collapsed"/>
<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}"/>
</StackPanel>
</DataTemplate>
<toolkit:ListPicker x:Name="_1stLanguageListPicker"
ItemTemplate="{StaticResource PickerItemTemplate}"
FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}"
Header="1st Specie Name Language"
FullModeHeader="1st Specie Name Language"
CacheMode="BitmapCache"/>
As you can see I show to user only the full name, hidding the code. But as a programmer I would like to use code name.
How to get to this name?
I have tried like that
if (_1stLanguageListPicker.SelectedIndex == 0)
{
firstlang = "GB"
}
But not working (some bool to int problem, normal = doesn't work also) and this is a bad way for many items list as you have to put many many IF statements.
What I understand is, Your problem is to get the data of the code TextBlock in the code behind.
First give some name to your TextBlock
<TextBlock x:Name="codeTextBlock" Text="{Binding code}" />
And then use the following code to access the 'code'
if (listPicker.SelectedIndex > -1)
{
var item = listPicker.ItemContainerGenerator.ContainerFromIndex(listPicker.SelectedIndex);
SearchVisualTree(item);
}
Here, SearchVisualTree() is:
private void SearchVisualTree(DependencyObject targetElement)
{
var count = VisualTreeHelper.GetChildrenCount(targetElement);
if (count == 0)
return;
for (int i = 0; i < count; i++)
{
var child = VisualTreeHelper.GetChild(targetElement, i);
if (child is TextBlock)
{
TextBlock targetItem = (TextBlock)child;
if (targetItem.Name == "codeTextBlock")
{
var code = targetItem.Text;
return;
}
}
else
{
SearchVisualTree(child);
}
}
}
One possible problem here is(which you faced), we cannot access the DataTemplate before ListPicker is Loaded. In other workds, before DataTemplate is generated for the ListPicker.
So the placement of the above code is very important.
Hope this helps

Bind web xml attributes in listbox

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.

Listpicker binding issue WP7

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.

Resources