How to bind array to Data template - List Box - c#

I am currently parsing an HTML to produce an array shown below in a listbox
I am outputing each object in the array to a textbox like so
//first line
flight1.Text = node[0][2];
origin1.Text = node[0][3];
status1.Text = node[0][7];
time1.Text = node[0][6];
//second line
textBlock1.Text = node[1][3];
textBlock2.Text = node[1][3];
textBlock4.Text = node[1][7];
textBlock3.Text = node[1][6];
This is the outcome
AS you can image this is fairly grueling work and probably not the best way to do it.
Is there anyway to Bind each section on the array eg.. all flight names to a listbox in a data template so it would automatically list all the flight times and all the flight times etc..
Something like this for the XAML
<ListBox Margin="6,6,-12,0" Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="Auto">
<TextBlock Text="{Binding FlightNumber}" Foreground="#FF4BCCF5" FontSize="24" />
<TextBlock Text="{Binding Origin}" TextWrapping="Wrap" FontSize="22" Foreground="#FF969696" />
<TextBlock Text="{Binding Time}" TextWrapping="Wrap" FontSize="20" Foreground="#FF05C16C" />
<TextBlock Text="{Binding Status}" TextWrapping="Wrap" FontSize="20" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

I'm not sure what you would like to achieve. But in general I would suggest to do the following steps:
Parse the HTML
Put each line in FlightInformation-Class (with properties like you have in your XAML-example)
Put all the FlightInformation-Objects in an ObservableCollection
Apply a DataTemplate to your ItemsControl

Related

Long text in tooltip is not fully shown in UWP

I've defined a for a listbox item, and binded the text inside the tooltip to two properties of the object (name + description) but i have an issue that the text is being cut off
here is my tooltip:
<ToolTipService.ToolTip>
<StackPanel >
<StackPanel Orientation="Vertical">
<TextBlock FontSize="13">
<Bold>Name</Bold>
</TextBlock>
<TextBlock Text="{x:Bind name}"/>
</StackPanel>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="13" TextWrapping="Wrap">
<Bold>Description</Bold>
</TextBlock>
<TextBlock Text="{x:Bind description}"/>
</StackPanel>
</StackPanel>
</ToolTipService.ToolTip>
Now the thing is, if i bind the tooltip to a method that returns the name + description (Which is how it was previously, but was super ugly) it does show all the text, it was like this:
<ToolTipService.ToolTip>
<TextBlock Text="{x:Bind Description}"/>
</ToolTipService.ToolTip>
But I needed to style it to make it look better, so i've tried to do what was posted above.
I've already tried setting the Width/Height to super large values, didn't do anything.
any ideas?
The tooltip template probably has a default maximum width, which cuts off the TextBlock. To solve this, just add TextWrapping attribute:
<TextBlock TextWrapping="Wrap" Text="{x:Bind description}"/>
Now the tooltip text will wrap on multiple lines as necessary

Move data from itemsource of one listbox to another listbox wp8

I am parsing a RSS Feed from one site and storing it in listbox1 and then parsing another RSS storing it in listbox2. Now I want to combine the data of listbox1 and listbox2 in listbox3. It might sound silly but I am unable to do it. The main problem is I am not able to access the controls inside the listbox.
<ListBox x:Name="list1" ItemsSource="{Binding RSSData}" DataContext="{Binding RSSData}" Visibility="Collapsed">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock HorizontalAlignment="Center" x:Name="txtpubDate" Foreground="#FF170505" Text="{Binding Path=pubDate}" TextDecorations="Underline" ></TextBlock>
<TextBlock Padding="18" Foreground="#FF0E0101" x:Name="txtTitle" TextWrapping="Wrap" Text="{Binding Path=Title}" FontWeight="Bold"></TextBlock>
<Image Height="200" x:Name="imageLink" Source="{Binding strImg}"></Image>
<TextBlock Foreground="#FF0F0202" Padding="35" TextTrimming="WordEllipsis" HorizontalAlignment="Left" x:Name="txtDesc" Margin="2" TextWrapping="Wrap" Text="{Binding Path=Description,Converter={StaticResource RssTextTrimmer}}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This is the XAML code of my listbox1 and I am trying to do it with listbox3.itemsource=listbox1.itemsource; but nothing is happening.
I also tried to add data in this way: listbox3.items.add(listbox.items[i])
but nothing is working.
Please help.
You can loop through the contents of the listbox and add them one by one for instance:
foreach(object item in listbox1.items)
{
listbox3.items.add(item);
}
and then do the same for listbox2.

How to Save to Database from ListBox?

I know how to save to my database from a datagrid's selected row, but I want to save all records from my list box to the database. Here is the related xaml for the ListBox:
<UserControl.Resources>
<DataTemplate x:Key="EventDataTemplate">
<StackPanel Name="OuterStackPanel">
<TextBox Text="{Binding Title}" Name="TextBoxTitle"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding BeginDate}" Name="TextBoxBeginDate"/>
<TextBlock Text=" - "/>
<TextBlock Text="{Binding EndDate}" Name="TextBoxEndDate"/>
</StackPanel>
<TextBox Text="{Binding Venue}" Name="TextBoxVenue"/>
<TextBox Text="{Binding Location}" Name="TextBoxLocation"/>
<TextBlock Name="TextBlockID" Text="{Binding id}"/>
</StackPanel>
</DataTemplate>
</UserControl.Resources>
<ListBox x:Name="AllEventsListBox" ItemTemplate="{StaticResource EventDataTemplate}"/>
With my DataGrid method, I use this codebehind to save its row's related TextBoxes to the database:
MyServiceClient client = new MyServiceClient();
client.UpdateEventCompleted += new EventHandler<UpdateEventCompletedEventArgs>(client_UpdateEventCompleted);
singleEvent.id = int.Parse(LblID.Content.ToString());
singleEvent.Title = TextBoxTitle.Text;
singleEvent.Location = TextBoxLocation.Text;
singleEvent.BeginDate = TextBoxBegin.Text;
singleEvent.EndDate= TextBoxEnd.Text;
singleEvent.Venue = TextBoxVenue.Text;
client.UpdateEventAsync(singleEvent);
However, I would like to save all of the altered text from my generated ListBoxItems, instead of just one row at a time from my DataGrid method.
Is this possible?

c# find texbox in WinPhone7 silverlight app

I have something like this:
<ListBox Height="456" Margin="30,113,0,0" x:Name="listBox1" Width="446" Background="Black">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28" Orientation="Horizontal">
<TextBlock Text="{Binding name}" FontSize="28" Padding="10" >
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener
Hold="GestureListenerHold" />
</toolkit:GestureService.GestureListener>
</TextBlock>
<TextBlock Text="{Binding id}" FontSize="24" Padding="10" >
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener
Hold="GestureListenerHold" />
</toolkit:GestureService.GestureListener>
</TextBlock>
<TextBlock Text="{Binding status}" FontSize="24" Padding="10">
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener
Hold="GestureListenerHold" />
</toolkit:GestureService.GestureListener>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and in my app i do:
data = (List<Device>)serializer.Deserialize(stream);
this.listBox1.ItemsSource = data;
on every textblock I have a gesture listener which should provide the user with the option to change 'name', so when they hold the textblock the app navigates him to another page where he fills in the form.
My question is how to find the texblock which is binding 'name' when I click and hold another texblock?
You could use Linq-to-VisualTree, a utility which I wrote which allows you to navigate the visual tree:
http://www.scottlogic.co.uk/blog/colin/2010/03/linq-to-visual-tree/
Firstly, name the TextBlock so that it can be uniquely identified:
<TextBlock x:Name="NameText" Text="{Binding name}" FontSize="28" Padding="10" />
Then, when one of your other TextBlocks is tapped, you can find it as follows:
// locate the parent stackpanel
var parentStackPanel = tappedTextBlock.Ancestors().First()
// locate the names TextBlock
var nameTextBlock = parentStackPanel.Elements()
.Where(el => el.Name == "NameText").Single();
Cast the sender in the GestureListenerHold method.

Accessing a TextBlock which is contained inside a ListBox

I have a textblock which is inside a listbox and I am trying to write an if statement which is dependant on the contents of this textblock. I am trying to get the data from the TextBlack which I have named "category1" however when I try to write my if statement I am getting a message which just says
"the name category1 does not exist in the current context"
I tired moving that TextBLock out of the ListBox and it works fine but wont work while its inside there. Does anyone know how to reference this textblock.
Here is the my XAML code
<ListBox x:Name="HINList" Margin="0,300,-12,0" ItemsSource="{Binding Details}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding HINNumber}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding CategoryLetter}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="category1" Text="{Binding Category1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="{Binding Category2}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="{Binding Category3}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextNormalStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Assuming you're writing your if statement in the code behind file, wouldn't something like:
if(((WhateverTypeIsInDetailsCollection)HINList.SelectedItem).Category1 == something) {
// then do whatever you want
}
As Russell pointed out there is a category1 item for every entry in the list. I assume you wanted to do something with the selected item.
This is due to xaml namescopes. The names inside a DataTemplate are in a different namescope than outside, that's why you can't access them (what #Russell pointed is part of why it's done this way).
I think that you want to access that field for the "Category1" property on the selected item of the HINList ListBox that is bound to the Details collection. What you can do is set the binding on the Category1 to be two way, and bind the SelectedItem of the ListBox to a Detail item like so:
xaml:
<ListBox x:Name="HINList" ItemsSource="{Binding Details}"
SelectedItem={Binding SelectedDetailItem, Mode=TwoWay}>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432">
<TextBlock Text="{Binding Category1, Mode=TwoWay}" TextWrapping="Wrap" .../>
<!-- the other fields -->
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
code-behind
if(SelectedDetailsItem.Category1==...)
{
....
}
Hope this helps :)

Categories

Resources