How to print all the values using linq? - linq

I have got a listbox and I have to populate that with the elements from the query. The query is,
var query = from b in context.table select b;
List<Tab> reclist = q.ToList();
using LINQ how can I print all the obtained values in the listbox?

You'll need to create a DataTemplate containing a TextBlock whose Text property you bind to a property on your object. So, some XAML something like;
<ListBox x:Name="MyListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Presuming your Tab class has a property called Name. Obviously if it doesn't you'll want to change the {Binding Name} portion of the XAML to match the property name that you want to display in the ListBox.
You then bind to your item;
MyListBox.ItemsSource = reclist;

Related

Is there a way I can pass a grid name or a parameter into a tapped event?

I have this code and somehow I want to get the text value of the first label to the tapped event in the CS code. As the tapped event is on the grid I had an idea of putting the text value into x:Name.
<ViewCell >
<Grid VerticalOptions="CenterAndExpand" x:Name="{Binding [0].Name}" Tapped="atiSelectValueX" >
<local:StyledLabel Text="{Binding [0].Name}" HorizontalOptions="StartAndExpand" />
<local:StyledLabel IsVisible="{Binding [0].IsSelected}" TextColor="Gray" HorizontalOptions="End" Text="x" />
</Grid>
</ViewCell>
The CS code I have so far:
void atiSelectValueX(object sender, EventArgs e)
{
var cell = sender as Grid;
if (cell == null)
return;
var selected = cell. <<< I want to get the name here
What I would like to do is to get the x:Name value in the CS code. I was hoping to get the sender information but it seems like I cannot enter
cell.Name
Is there another way I can get a parameter like the name (which is the same as the text in the first label always) in the C# code?
The x:Name is metadata used by the XAML tools, not an actual property of the object. You could also bind Name to an unused property, like StyleID, and access that instead
<Grid VerticalOptions="CenterAndExpand" StyleId="{Binding [0].Name}" ...
and then
var selected = cell.StyleId;

assigning the result of variable into textblock

I'm trying to get the result of variable into a textblock.text proprety, I'm using
this code:
bool isavailable = NetworkInterface.GetIsNetworkAvailable();
result = isavailable.ToString();
<TextBlock Height="62" HorizontalAlignment="Left" Margin="12,60,0,0"
Name="textBlock1" Text="{Binding result}" VerticalAlignment="Top"
Width="400" Foreground="White" TextWrapping="Wrap" />`
Do you need to keep the binding? The simplest approach here would be to simply use:
textBlock1.Text = result;
However, the better approach would be to use a ViewModel implementing INotifyPropertyChanged with an appropriate property. Then you would make your TextBlock bind to the property, and set the property from your code. The property would raise the appropriate event, and the UI would update accordingly.

WP7 - accessing selected item in listbox when listbox is within an itemtemplate

I have a Pivot item template that includes a listbox
<controls:Pivot x:Name="MainPivot" ItemsSource="{Binding PivotItemHeaders}" Title="CLASS TIMETABLE" >
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}"/>
</DataTemplate>
</controls:Pivot.HeaderTemplate>
<controls:Pivot.ItemTemplate>
<DataTemplate>
<ListBox x:Name="Events" ItemsSource="{Binding allEventItems}" ItemTemplate="{StaticResource EventDisplay2}"/>
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>
In the code behind I want to access the selectedItem of that listbox but I cannot 'get' to the listbox as such because ity is (presumably) within the template
i.e
this.NavigationService.Navigate(new Uri("/View/EventEdit.xaml?selectedEvent=" + Events.SelectedItem, UriKind.Relative));
The Events listbox is not being recognised.
Assuminh I can pass get the object and pass it through as a parameter, what code can I use to retrieve it
I know its starts with
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (NavigationContext.QueryString.ContainsKey("SelectedEvent"))
{
But I am unsure of the syntax/code to extract out the object from the parameters
Appreciate how I can get the selectedItem from this listbox and the code to get the object being passed through
thanks
Rather than attempt to access the ListBox, you could use the SelectionChanged event to be told when the value changes:
<ListBox x:Name="Events"
ItemsSource="{Binding allEventItems}"
ItemTemplate="{StaticResource EventDisplay2}"
SelectionChanged="Event_SelectionChanged" />
And then in your code behind:
private void Event_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.selectedEvent = (EventItem)e.AddedItems[0];
}
You can access the value using NavigationContext.QueryString["selectedEvent"], but you can only store strings in navigation query strings. If your listbox is currently bound to objects, you'll need to select a key and then find that event from the second page using that key.

Databinding PivotItems to ObservableCollection on WP7

I want to databind an ObservableCollection to a Pivot contronl in WP7 so that each object in my ObservableCollection becomes a PivotItem. This is the code I use:
<controls:Pivot x:Name="MainPivot" ItemsSource="{Binding Persons}">
<controls:Pivot.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding FullName}"/>
</DataTemplate>
</controls:Pivot.HeaderTemplate>
<controls:Pivot.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text="{Binding HomeTown}"/>
</StackPanel>
</DataTemplate>
</controls:Pivot.ItemTemplate>
</controls:Pivot>
This works and with tre items in my ObservableCollection I get three PivotItems. But when everything gets loaded the binding inside the DataTemplate won´t get updated. It is only when I scroll to the next PivotItem that the FirstName, LastName and HomeTown gets loaded.
Why is that? What am I missing?
Thanks
Check this discussion:
DataBound Pivot control is not creating the first PivotItem
I had the same problem, but the workaround with setting SelectedIndex=1 didn't suit me.
I found the other solution: when you adding the Item to your Persons collection you should first create a temp element and only when you fill all data add it to your Persons collection.
Person tempPers = new Person() { FullName = "Abduvaliev Edem", FirstName = "Edem", LastName = "Abduvaliev", HomeTown = "Sevastopol"};
Pesrons.Add(tempPers);
After doing a simple test i cannot reproduce this behavior. I i put a breakpoint inside the get block of the equivalent of FirstName with two items in my ObservableCollection i get two hits.
How did you detect that it is not bound? You cannot see the "next" pivotitems content, so how?
Sounds like there's some problem with the loading order - or with the notification code.
Check that you are correctly firing the PropertyChanged event when you set the properties on each of your FirstName, LastName and HomeTown members.
Here's how I do it. The problem for me is that the collection updates asynchronously in response to a web method call.
void Page_Loaded(object sender, RoutedEventArgs e)
{
_log.Debug("Page loaded.");
var vm = this.GetViewModel<TrendsViewModel>();
if (!vm.IsInitialized)
{
vm.PivotItems.CollectionChanged += (origin, args) =>
{
this.PivotControl.DataContext = null;
this.PivotControl.DataContext = vm;
};
vm.Initialize(this);
}
}
The key is hooking up an observer of the collection to which the Pivot items are bound and giving the data context a shake when it updates.

Passing Object rather than selectedIndex from Databound listbox

I'm having an issue when I try to pass the selectedIndex of my list from my "List" screen.
On my List screen I have the following binding:
Code Behind:
lbPrograms.ItemsSource = App.ViewModel.Items;
XAML:
<ListBox x:Name="lbPrograms" ItemsSource="{Binding Items}" SelectionChanged="lbPrograms_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
<Image x:Name="ItemImage" Source="/images/ArrowImg.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>
<StackPanel>
<TextBlock x:Name="ItemText" Text="{Binding programName}" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock x:Name="DetailsText" Text="{Binding createDate}" Margin="0,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
<TextBlock x:Name="programId" Text="{Binding programId}" Margin="0,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
<!--<Image x:Name="ItemFavs" Source="/images/favs.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>
<Image x:Name="ItemDelete" Source="/images/delete.png" Height="43" Width="43" VerticalAlignment="Top" Margin="10,0,20,0"/>-->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
In my detail screen I look up using the selectedIndex value:
App.ViewModel.Refresh();
DataContext = App.ViewModel.Items[index].nValDictionary;
However, since I can't figure out how to just update a value in my iEnumerable collection I have been removingAt[index] and then re-adding to the collection.
So can anyone tell me, how do use update an existing element in my collection? If not, can I pass the programId (that is in my binding) instead of the selectedIndex as the indexes are getting all messed up after the Delete/Add functionality.
Please Advise.
UPDATE:
After speaking in several forums I should clear up that I am implement INotifyChanged Event on my properties in my object.
Basically the following snippet of code is my current issue:
private void FavBtn_Click(object sender, EventArgs e)
{
foreach (var item in App.ViewModel.Items)
{
if ((item.currentProgram == true) && (item.programId != index))
{
item.currentProgram = false;
}
if (item.programId == index)
{
item.currentProgram = true;
}
}
When I run this everything looks to be ok.
However, when I navigate to another page, I re-load the object and the changes are lost. It is almost like I need to save them before navigating, however, if I do a item.Save(); I get duplicates in my list.
I'm guessing you haven't implemented INotifyPropertyChanged on your Items class yet.
Implementing this will allow updates made in these objects to be updated through your data bindings.
Here's a walkthrough on how to implement INotifyPropertyChanged.
How to: Implement the INotifyPropertyChanged Interface
Is App.ViewModel.Items of type ObservableCollection<T>?
If so, when you modify a property of one of the collection items and raise a property change event (via INotifyPropertyChanged) indicating the collection property that your ListBox.ItemsSource is bound to, the DataTemplate bindings will do the rest.

Resources