I am working on the Windows phone 8 application for the 1st time, and I am not that familiar with the controls and terms so please pardon me.
I have a windows phone 8 app which need to display a list of books ie: display book-title and book-desc in a list format.
I have used Windows phone DataBoundApp template for the project. I am trying to bind the grid with the database values.
Here is my code:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
using(Context c=new Context(Context.ConnectionString))
{
c.LogDebug = true;
char[] allchar = new char[4] { 'A', 'B','C','D' };
List<DB.Books> keys = new List<DB.Books>();
var exampleDictionary = new Dictionary<List<DB.Books>, string>();
for (int i = 0; i <= 3; i++)
{
var book = from m in c.Books
where m.Book_author.StartsWith(allchar[i].ToString())
orderby Convert.ToChar(m.Book_author)
select m;
keys.AddRange(book.ToList());
exampleDictionary.Add(book.ToList(), allchar[i].ToString());
}
MainLongListSelector.ItemsSource = exampleDictionary.ToList();
}
}
And Xaml source:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:LongListSelector x:Name="MainLongListSelector" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="MainLongListSelector_SelectionChanged">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17">
<TextBlock Text="{Binding Path=Value}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Grid.Column="1"/>
<TextBlock Text="{Binding Path=Key[1].Book_author}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}" Grid.Column="2"/>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>
Here I am trying to list the books in Alphabetical order, so the design should be as follows:
A:
book1-title,book1-desc
book2-title,book2-desc
book3-title,book3-desc
B:
book1-title,book1-desc
book2-title,book2-desc
book3-title,book3-desc
...
Please help me out. Thanks in advance.
Related
very new to C# WPF/WIN UI. My combobox displays my collection perfectly. But now when I want to do something with what the user selected I can't find the correct syntax in my 'SelectionChanged' event handler. I want to get the 'Market_ID' somehow. (FYI, I am not using MVVM yet as I don't understand how to implement, but I will learn. (really liking c#))
<ComboBox x:Name="cmbMarketID" PlaceholderText="Select Market ID" Width="500" Margin="5,5,0,0" RelativePanel.RightOf="border1" RelativePanel.Below="cmbState" ItemsSource="{x:Bind marketIdent}" SelectionChanged="cmbMarketID_SelectionChanged" SelectedItem="{Binding Market_ID}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="cmbo_market_ID" Text="{Binding Market_ID}" Width="15" TextAlignment="Right"/>
<TextBlock Text="{Binding Product}" Width="145" Margin="10,0,10,0" FontWeight="SemiBold"/>
<TextBlock Text="{Binding Company}" Width="70" Margin="10,0,10,0"/>
<TextBlock Text="{Binding System}" Margin="10,0,10,0"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Here is the event handler: (I used a simple string first, and that worked, but now I need to use a typed-collection)
private void cmbMarketID_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (cmbMarketID.SelectedIndex == -1)
{
// Do not execute event
}
else
{
//string mktid = cmbMarketID.SelectedItem.ToString().Substring(0, 2).TrimEnd();
//string mktid = cmbMarketID.SelectedItem;
int mktid = (int)cmbMarketID.SelectedItem(); <-------what should the correct syntax be here?
//v_metric_mktid = mktid;
}
}
Cast the SelectedItem property to your type.
For example, if marketIdent is an IEnumerable<YourClass>:
var item = cmbMarketID.SelectedItem as YourClass;
Or if marketIdent is an IEnumerable<int>:
if (mbMarketID.SelectedItem != null)
{
var mktid = (int)mbMarketID.SelectedItem;
}
I am trying to delete items from my list on click of delete button but clicking once the list only gets refreshed and clicking again deletes item from the list.
Here is my code:
private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
var query = BreakfastList.SelectedItems;
foreach (var item in query)
{
conn.Delete(item);
}
if (Item1.IsSelected)
{
List<DbManager> people = (from p in conn.Table<DbManager>()
select p).OrderByDescending(q => q.id).ToList();
BreakfastList.ItemsSource = people;
}
else if (Item2.IsSelected)
{
List<DbManager> people = conn.Table<DbManager>().OrderByDescending(q => q.id).Where(q => q.Reading == "Breakfast").ToList();
BreakfastList.ItemsSource = people;
}
else if (Item3.IsSelected)
{
List<DbManager> people = conn.Table<DbManager>().OrderByDescending(q => q.id).Where(q => q.Reading == "Lunch").ToList();
BreakfastList.ItemsSource = people;
}
else if (Item4.IsSelected)
{
List<DbManager> people = conn.Table<DbManager>().OrderByDescending(q => q.id).Where(q => q.Reading == "Dinner").ToList();
BreakfastList.ItemsSource = people;
}
}
Item1,2,3,4 are items in my combobox
My XAML for the list is:
<ListView
ScrollViewer.HorizontalScrollMode="Auto"
ScrollViewer.VerticalScrollMode="Enabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto"
Margin="0,0,0,0"
Name="BreakfastList"
Visibility="Visible"
>
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:DbManager" >
<StackPanel Orientation="Horizontal" Width="Auto"
HorizontalAlignment="Left" VerticalAlignment="Center"
Height="Auto">
<StackPanel Width="50" Height="Auto" Name="GluStack" RelativePanel.AlignLeftWithPanel="True" >
<TextBlock Text="{Binding Glucose}" Margin="0,0,10,0" IsTextScaleFactorEnabled="False" />
</StackPanel>
<StackPanel Width="80" Height="Auto" Name="ReadStack" RelativePanel.RightOf="GluStack">
<TextBlock Text="{Binding Reading}" Margin="0,0,10,0" IsTextScaleFactorEnabled="False"/>
</StackPanel>
<StackPanel Width="130" Name="Read1Stack" Height="Auto" RelativePanel.RightOf="ReadStack">
<TextBlock Text="{Binding Reading1}" Margin="0,0,10,0" IsTextScaleFactorEnabled="False" />
</StackPanel>
<StackPanel Width="Auto" Name="DateStack" RelativePanel.RightOf="Read1Stack"
Height="Auto">
<TextBlock Text="{Binding Date}" IsTextScaleFactorEnabled="False" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Well, I'm not sure as this is not enough information, but based on your description the first time your method runs (first click), BreakfastList.SelectedItems is empty; therefore nothing gets deleted. Then you update the list by setting the ItemsSource on it. After setting the ItemsSource, BreakfastList.SelectedItems will be populated and that's why it works the second time.
If you set a breakpoint on:
var query = BreakfastList.SelectedItems;
Could you check if SelectedItems contains elements? Or in the next line if query has any?
Also, can you please give the xaml for BreakfastList? How is it initialized the first time?
(Sorry I'm writing this as an answer, but I don't have enough points to be able to write a comment on your question.)
I have implemented textblock in Listbox ItemTemplate/data template. It is databound to "Data" which is getting read from an xml file. Everything works fine but, the text is getting trimmed in textblocks and a there ia a blank space. I know the reason and it is due to limitations in textblock. Now i tried to use, Scrollable TextBlock for WP7 from Alex Yakhnin's Blog but I am not able to understand how to implement this in my project?
C# CODE:
string XMLTagNameBody = "page";
private void ParseHeaderBody()
{
XDocument SunderGutkaXMLInstance = XDocument.Load(xmlFileToOpen);
var XMLBody = from query in SunderGutkaXMLInstance.Descendants(XMLTagNameBody)
select new BodyContentClass
{
Data = (string)query.Element("data"),
};
ListBoxDisplayContent.ItemsSource = XMLBody;
}
public class BodyContentClass
{
string book;
string data;
public string Book
{
get { return book; }
set { book = value; }
}
public string Data
{
get { return data; }
set { data = value; }
}
}
XAML Code:
<ListBox Margin="0,25,0,32"
x:Name="ListBoxDisplayContent"
FontSize="48"
HorizontalAlignment="Center">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="ListBoxDisplayContentStackPanel"
Margin="10"
HorizontalAlignment="Center">
<TextBlock x:Name="MainContentDisplayTextBlock"
TextWrapping="Wrap"
Text="{Binding Data}"
TextAlignment="Center"
HorizontalAlignment="Center" />
<Image x:Name="dividerImage"
Stretch="Fill"
Source="/Assets/MainContentDivider.png"
Opacity="1"></Image>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Links for Alex Yakhnin's Blog/Scrollable TextBlock for WP7:
http://social.msdn.microsoft.com/Forums/windowsapps/en-US/3af9bc99-2526-49fc-b4a5-4170e12d11ab/do-textblocks-have-a-max-line-limit-if-so-how-do-i-increase-it?forum=wpdevelop
http://blogs.msdn.com/b/priozersk/archive/2010/09/08/creating-scrollable-textblock-for-wp7.aspx
You can create a custom control and than it can be added to your project like:
<StackPanel x:Name="ListBoxDisplayContentStackPanel" Margin="10"
HorizontalAlignment="Center">
<my:ScrollableTextBlock Text="{Binding Data}"
HorizontalAlignment="Left" Name="scrollableTextBlock1"
VerticalAlignment="Top" Height="618" Width="427" Margin="12,-11,0,0" />
<TextBlock x:Name="MainContentDisplayTextBlock"
TextWrapping="Wrap"
TextAlignment="Center"
HorizontalAlignment="Center" />
<Image x:Name="dividerImage"
Stretch="Fill"
Source="/Assets/MainContentDivider.png"
Opacity="1"></Image>
</StackPanel>
Where my is the namespace of your control.
This is the first time i am doing xaml so please understand that I might be slow in learning
Below is my CS codes. I am trying to bind the "attributes" to listbox.
public DirectionPage()
{
InitializeComponent();
List<Feature> features = App.dResult.directions[0].features;
foreach (Feature f in features)
{
Attributes a = f.attributes;
MessageBox.Show(a.text);
}
}
public ObservableCollection<Feature> test = new ObservableCollection<Feature>();
Below is the XAML codes.
<ListBox x:Name="directionListBox" ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding text}" Style="{StaticResource PhoneTextTitle2Style}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Any help will be greatly appreciated.
I didn't see the collection of attributes.
Possibly what you can do is collect the attributes in a list may be your test and put it in the biding.
Or put the Features collection as itemsource of your list box.
i.e.
public DirectionPage()
{
InitializeComponent();
List<Attributes> LAtributes=new List<Attributes>();
List<Feature> features = App.dResult.directions[0].features;
foreach (Feature f in features)
{
Attributes a=new Attributes();
a = f.attributes;
LAttributes.add(a);
MessageBox.Show(a.text);
}
directionListBox.ItemsSource=Lattribute;
}
and
<ListBox x:Name="directionListBox" ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=text}" Style="{StaticResource PhoneTextTitle2Style}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Hopefully this will help you!
I have a listbox in WPF databinded to a observablecollection
<ListBox Margin="0,0,-12,0" ItemsSource="{Binding ShopList}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Grid.Row="1"
Grid.ColumnSpan="2" KeyDown="ListBox_KeyDown" KeyUp="ListBox_KeyUp"
>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17" >
<!--Replace rectangle with image-->
<Rectangle Height="50" Width="50" Stroke="Black" StrokeThickness="6" Margin="12,0,9,0"/>
<StackPanel Width="Auto">
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding Quantity}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I then have a filter method I want called
private void ShopItemDay_Filter(object sender, FilterEventArgs e)
{
var item = e.Item as ShopItem;
e.Accepted = item.Day == 1;
}
But I cant find any properties on the Listbox to use a filter method like done here http://www.galasoft.ch/mydotnet/articles/article-2007081301.aspx
You need to create a 'view' on your collection. See the documentation for CollectionView.Filter. The framework will create a default view for all bound collections. You can add a filter as follows:
ICollectionView _customerView = CollectionViewSource.GetDefaultView(customers);
_customerView.Filter = CustomerFilter
private bool CustomerFilter(object item)
{
Customer customer = item as Customer;
return customer.Name.Contains( _filterString );
}
(From this tutorial);
As you can see in the article you linked to, the filter is not a property of the Control. It is a property of the CollectionViewSource which is a kind of wrapper around the collection. This wrapper allows for sorting, grouping and filtering.