ListBox items are clickable but not visible WP7 - windows-phone-7

I'm making a Windows Phone 7 app.
I have string items being added to a ListBox.
When I run the app, the items in the ListBox are clickable, but I cannot actually see them.
They are set to visible and white (black background).
Does anyone know what might be causing this?
Here is my ListBox code:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" ShowGridLines="False">
<StackPanel>
<StackPanel Orientation="Horizontal">
<Button Content="i" Height="72" HorizontalAlignment="Left" Margin="0,0,0,0" Name="informationButton" VerticalAlignment="Top" Width="70" FontFamily="Georgia" DataContext="{Binding}" Click="informationButton_Click" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="-10,0,0,0" Name="searchBox1" Text="Search..." VerticalAlignment="Top" Width="336" TextChanged="searchBox1_TextChanged" />
<Button Content="→" Height="72" HorizontalAlignment="Right" Margin="-15,0,0,0" Name="searchButton" VerticalAlignment="Top" Width="74" Click="button1_Click_1" />
</StackPanel>
<ListBox Height="533" Name="listBox" Width="452" ScrollViewer.VerticalScrollBarVisibility="Visible" DoubleTap="textBlock_DoubleTap" SelectionChanged="listBox_SelectionChanged" Padding="2,5">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Height="30" Text="" Margin="2,10" Name="textBlock1" DoubleTap="TextBlock_DoubleTap" Visibility="Visible"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Grid>
Here is the XAML where the items are added. The string part works.
try
{
if (root2.getSOCSectionListByNbrResponse.ClassOffered.ClassNumber != null)
text = text + root2.getSOCSectionListByNbrResponse.ClassOffered.ClassNumber;
if (root2.getSOCSectionListByNbrResponse.ClassOffered.SectionNumber != null)
text = text + " " + root2.getSOCSectionListByNbrResponse.ClassOffered.SectionNumber;
if (root2.getSOCSectionListByNbrResponse.ClassOffered.CourseDescr != null)
text = text + " " + root2.getSOCSectionListByNbrResponse.ClassOffered.CourseDescr;
if (root2.getSOCSectionListByNbrResponse.ClassOffered.SectionTypeDescr != null)
text = text + " " + root2.getSOCSectionListByNbrResponse.ClassOffered.SectionTypeDescr;
if (root2.getSOCSectionListByNbrResponse.ClassOffered.Meeting.Days != null)
text = text + " " + root2.getSOCSectionListByNbrResponse.ClassOffered.Meeting.Days;
if (root2.getSOCSectionListByNbrResponse.ClassOffered.Meeting.Times != null)
text = text + " " + root2.getSOCSectionListByNbrResponse.ClassOffered.Meeting.Times;
if (root2.getSOCSectionListByNbrResponse.ClassOffered.Instructor.InstructorName != null)
text = text + " " + root2.getSOCSectionListByNbrResponse.ClassOffered.Instructor.InstructorName;
if (root2.getSOCSectionListByNbrResponse.ClassOffered.EnrollmentStatus != null)
text = text + " " + root2.getSOCSectionListByNbrResponse.ClassOffered.EnrollmentStatus;
}
catch (Exception e)
{
}
listBox.Items.Add(text);

I just looked through the code you posted and I found the solution:
Replace this:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Height="30" Text="" Margin="2,10" Name="textBlock1" DoubleTap="TextBlock_DoubleTap" Visibility="Visible"/>
</StackPanel>
</DataTemplate>
with this:
<DataTemplate>
<TextBlock
Height="30" Text="{Binding}" Margin="2,10"
Name="textBlock1" DoubleTap="TextBlock_DoubleTap"
Visibility="Visible"/>
</DataTemplate>

Related

Getting information from child element of stackpanel within listbox

I know there should be a simple solution to this question but I just cant seem to figure it out here is what my code looks like:
<ListBox HorizontalAlignment="Left"
x:Name="locationsNB"
VerticalAlignment="Top"
Height="563"
Width="455"
SelectionChanged="locationsNB_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"
Margin="18,0,0,0"
x:Name="placeDetails">
<Image Source="{Binding icon}"
Height="40"
Width="40"
VerticalAlignment="Top"
Margin="0,10,8,0" />
<StackPanel Width="350">
<TextBlock Text="{Binding name}"
FontSize="35"
Foreground="#399B81"
TextWrapping="Wrap" />
<TextBlock Text="{Binding vicinity}"
FontSize="20"
Foreground="#888888"
TextWrapping="Wrap" />
<TextBlock x:Name="reference"
Text="{Binding reference}"
Visibility="Collapsed" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I want to get the stackpanel->reference text (Text="{Binding reference}") of the selected item I dont know what my C# should look like but any help will be greatly appreciated.
If the ItemsSource of your ListBox is bound to a collection of items then you can use the SelectedItem property of the ListBox
private void locationsNB_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listbox = (ListBox)sender;
var myObject = listbox.SelectedItem as MyCustomObject;
if (myObject == null) return;
// perform your custom logic with this item
}

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

Getting the values of TextBox from a data bound ListBox

I need to get the values from Listbox selected items. Note that, the data templates are in data bound. here is the xaml:
<ListBox Name="AppointmentResultsData" ItemsSource="{Binding}" Height="650" Width="480" Margin="24,0,0,0" Foreground="#CBF7FA" SelectionChanged="AppointmentResultsData_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Path=Subject, Mode=TwoWay}" TextWrapping="Wrap" FontSize="30" Grid.Column="0" Grid.Row="1"/>
<Grid Grid.Column="0" Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Path=Account.Name}" Grid.Column="0" Grid.Row="1" FontSize="28"/>
<TextBlock Text="Start : " Grid.Column="0" FontSize="22" Grid.Row="2"/>
<TextBlock Text="{Binding Path=StartTime}" FontSize="22" Grid.Column="1" Grid.Row="2"/>
<TextBlock Text="End : " Grid.Column="0" Grid.Row="3" FontSize="22"/>
<TextBlock Text="{Binding Path=EndTime}" Grid.Column="1" FontSize="22" Grid.Row="3"/>
<TextBlock Text="Location : " Grid.Column="0" Grid.Row="4" FontSize="22"/>
<TextBlock Text="{Binding Path=Location}" Grid.Column="1" FontSize="22" Grid.Row="4"/>
<TextBlock Text="Status : " Grid.Column="0" FontSize="22" Grid.Row="5"/>
<TextBlock Text="{Binding Path=Status}" Grid.Column="1" FontSize="22" Grid.Row="5"/>
</Grid>
<TextBlock Text=" "/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I need values of the textboxes in selection changed event.I have tried like this...
private void AppointmentResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//SelectedEvent seleted = AppointmentResultsData.SelectedItem as SelectedEvent;
if (AppointmentResultsData.SelectedIndex == -1)
return;
ListBoxItem currentSelectedListBoxItem = this.AppointmentResultsData.ItemContainerGenerator.ContainerFromIndex(AppointmentResultsData.SelectedIndex) as ListBoxItem;
if (currentSelectedListBoxItem == null)
return;
// Iterate whole listbox tree and search for this items
TextBox nameBox = helperClass.FindDescendant<TextBox>(currentSelectedListBoxItem);
TextBlock nameBlock = helperClass.FindDescendant<TextBlock>(currentSelectedListBoxItem);
MessageBox.Show(nameBlock.Text + " " + nameBox.Text);
}
But it didn't work !
Solved it !
private void AppointmentResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBoxItem = AppointmentResultsData.ItemContainerGenerator.ContainerFromIndex(AppointmentResultsData.SelectedIndex) as ListBoxItem;
var txtBlk = FindVisualChildByType<TextBlock>(listBoxItem, "txtLocation");
MessageBox.Show(txtBlk.Text);
}
T FindVisualChildByType<T>(DependencyObject element, String name) where T : class
{
if (element is T && (element as FrameworkElement).Name == name)
return element as T;
int childcount = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < childcount; i++)
{
T childElement = FindVisualChildByType<T>(VisualTreeHelper.GetChild(element, i), name);
if (childElement != null)
return childElement;
}
return null;
}
Suppose you have a list of class(MyClass) objects which you have databinded to listbox
Add a handler gesturelistener tap to the datatemplate
In the handler do this:
private void ItemClickedEventHandler(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
{
MyClass clickedMyclass = (MyClass)((System.Windows.Controls.Grid)sender).DataContext;
}
you have the object of the current selected item and you can access all the class variables. eg(StartTime etc.)
Well you are casting it to the wrong type, this should work :
private void AppointmentResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var listBoxItem = AppointmentResultsData.SelectedItem as ListBoxItem;
TextBox nameBox = listBoxItem .FindName("nameYourTextBox") as TextBox;
TextBlock nameBlock = dd.FindName("nameYourTextBlock") as TextBlock;
MessageBox.Show(nameBlock.Text + " " + nameBox.Text);
}
of course you need to add the Name to your TextBox and TextBlock
<TextBlock x:Name="nameYourTextBlock Text="{Binding Path=Account.Name}" Grid.Column="0" Grid.Row="1" FontSize="28"/>
Plus I don't see any TextBox in your XAML.

NullReferenceException when i try to retrieve object from a listboxitem with contextmenu after performed a page navigation

I've a ListBox with a ContextMenu inside.
XAML
<ListBox ItemsSource="{Binding}"
Name="recipesListBox"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Grid.Row="1"
Margin="12,0,12,0"
Loaded="recipesListBox_Loaded"
SelectionChanged="recipesListBox_SelectionChanged" >
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Name="listBoxItemBoarder" BorderBrush="White" BorderThickness="1" Margin="0,0,0,10"
Background="Black" Height="118" HorizontalAlignment="Stretch" >
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Canvas>
<Image Source="{Binding Image}" Height="116" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
</Canvas>
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Margin="100,0,0,0">
<TextBlock Text="{Binding Name}" FontWeight="Bold" FontSize="22" />
<TextBlock Text="{Binding Category}" />
<TextBlock Text="{Binding IngredientsStringWithoutAmounts}" Width="329" TextWrapping="Wrap" Height="56" TextTrimming="WordEllipsis" FontStyle="Italic" />
</StackPanel>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu Name="allCocktailsContextMenu">
<toolkit:MenuItem Header="{Binding Path=AppResources.MainPage_PivotItem_AllCocktails_allCocktailsContextMenu_Header_AddFav, Source={StaticResource LocalizedStrings}}" Click="MenuItem_Click" />
<toolkit:MenuItem Header="{Binding Path=AppResources.MainPage_PivotItem_AllCocktails_allCocktailsContextMenu_Header_MarkTried, Source={StaticResource LocalizedStrings}}" Click="MenuItem_Click" />
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C#
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem menuItem = (MenuItem)sender;
ListBoxItem selectedListBoxItem = this.recipesListBox.ItemContainerGenerator.ContainerFromItem((sender as MenuItem).DataContext) as ListBoxItem;
string message = "You pressed " + menuItem.Header.ToString() + ".\n";
try
{
Recipe selectedRecipe = selectedListBoxItem.DataContext as Recipe;
message += "Selected recipe: " + selectedRecipe.Name + ", " + selectedRecipe.ID;
}
catch (NullReferenceException exc)
{
Debug.WriteLine(exc.ToString());
message += "NullReferenceException :(";
}
MessageBox.Show(message, "Info", MessageBoxButton.OK);
}
The ListBox is in the MainPage of my app, and when I try to retrieve the object binded with ContextMenu everything works fine but, when I perform a page navigation and after go back to my MainPage, if I try to retrieve an object again I get a NullReferenceException. I'm really young in WP coding, and I'm not using the MVVM pattern, is this my error?
Which object are you getting a Null on? It may be your DataContext but without the erred object it's hard to tell

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.

Resources