I am trying to bind a listbox from 2 tables. These 2 tables are related.
(windows phone project)
XAML:
<ListBox Name="LstOrders" ItemsSource="{Binding}" Margin="12,11,12,12" toolkit:TiltEffect.IsTiltEnabled="True" Height="643">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0">
<TextBlock Text="{Binding refe}"
Tag="{Binding idOrder}"
Margin="0,0,0,0"
FontSize="{StaticResource PhoneFontSizeExtraLarge}"
FontFamily="{StaticResource PhoneFontFamilySemiLight}"/>
<TextBlock Text="{Binding tipo}"
Margin="0,0,0,0"
Foreground="{StaticResource PhoneSubtleBrush}"
FontSize="{StaticResource PhoneFontSizeNormal}"/>
<TextBlock Text="{Binding country}"
Margin="0,0,0,0"
Foreground="{StaticResource PhoneSubtleBrush}"
FontSize="{StaticResource PhoneFontSizeNormal}"
FontFamily="{StaticResource PhoneFontFamilySemiBold}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C#
EXAMPLE
using (ordersDBDataContext miDataContext = new ordersDBDataContext("Data Source='isostore:/mydatabase.sdf'"))
{
var _lista = from p in miDataContext.orders
join t in miDataContext.tipos on p.idTipo equals t.idTipo
orderby p.refe
where p.idCliente == _idCli
select new
{
p.refe, p.country,p.idOrder,t.tipo
};
this.LstOrders.ItemsSource = _lista;
}
RESULT
No display any data.What is wrong?
iF i doing this I can see that _lista contains correct data:
foreach (var rr in _lista)
{
MessageBox.Show(rr.tipo);
}
You are disposing your DataContext (correctly), but this means when the _lista query is executed the DataContext will no longer be valid and there will be an exception. WPF unhelpfully swallows exceptions in certain circumstances so you probably aren't seeing the exception.
The solution is to use:
this.LstOrders.ItemsSource = _lista.ToList();
and also to either remove ItemsSource={Binding} from your xaml or alternatively leave the binding in and use
this.LstOrders.DataContext = _lista.ToList();
Also, see Silverlight 4 Data Binding with anonymous types which may be relevant to your problem.
I don't know the context of your code snippet but I'm guessing you probably need to call
this.LstOrders.Items.Refresh();
I cant exactly explain why cos I am not familiar with WPF, but in winforms it would have been a DataBind().
Related
when i run program, listbox not show nothing.
please give me correct code.
<StackPanel x:Name="ContentPanel1" Grid.Row="1" Margin="12,0,12,0">
<ListBox x:Name="MultiList" FontSize="26" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=id}" Width="150"/>
<TextBlock Text="{Binding Path=project_id}"/>
<Button Click="Button_Click" Content="button"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock x:Name="txtDay" />
<TextBlock x:Name="txtTemp"/>
</StackPanel>
//-------------------------
phoneDBContext db = new phoneDBContext("Data Source='appdata:/phoneDB.sdf'; File Mode = Read Write;");
var q1 = from p in db.Projects
select p;
MultiList1.ItemsSource = q1.ToList();
Try without "Path=". Example: Text="{Binding id}"
I found the error. The first letter of each field has to be uppercase.
I wasted one week time on this problem.
It shows a bug in the IntelliSense editor of Microsoft Visual Studio 2013.
I hope Microsoft will correct this problem.
<TextBlock Text="{Binding Path=Id}" Width="150"/>
I'm trying to do some XAML binding for the Windows Phone (targeting WP7.1) and I have a collection of checkboxes that I want to display. I want to put them inside the WrapPanel
What control(s) would I use to bind to a collection of checkboxes? I don't see an ItemSource for the WrapPanel. So I'm not sure what I would use.
<ListBox Height="auto" Name="lbAssignments" BorderThickness="1" BorderBrush="Black" ItemsSource="{Binding DataList}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock x:Name="TextBlock" Text="{Binding Title}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0,0,10" FontSize="26.667" TextWrapping="Wrap"/>
<TextBlock x:Name="TextBlock1" Text="{Binding Title}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0,0,10" FontSize="26.667" TextWrapping="Wrap"/>
<toolkit:WrapPanel Height="400" Width="400">
<!--collection of checkboxes-->
</toolkit:WrapPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Binding to a collection of UI controls is not exactly the thing you'd want to do. Instead, I would recommend binding to a collection. For many reasons - performance, memory allocation and general maintenance/flexibility.
Since you mentioned that you have a List<string>, you can just bind it to a ListBox:
<ListBox ItemsSource="{Binding YourList}">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding}"></CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
However, you can still proceed with biding an element to a collection of other elements, given that appropriate support exists. You could use an ItemsControl:
<ItemsControl ItemsSource="{Binding ElementName=myPage, Path=SomeCollection}">
</ItemsControl>
Here, SomeCollection might as well be ObservableCollection<CheckBox>.
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) };
i'm new to windows phone 7 and i have a webservice which returns data from a sql database.
I'm display the data in a gridview in asp.net.
Now i want to make the same in windows phone 7
Which control to use to show the records and how?
Thank you very much
There is no out of the box datagrid control on windows phone 7.
This is because it is really hard to got a lot of data in a grid readable on a small screen as a phone. If you want to have this anyway you have to build your own.
You can use a listbox as some soft of grid like this:
<ListBox x:Name="myListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock width="100" Text="{Binding Field1}"/>
<TextBlock width="100" Text="{Binding Field2}"/>
<TextBlock width="100" Text="{Binding Field3}"/>
<TextBlock width="100" Text="{Binding Field4}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
If this isn't what you're looking for you could maybe have a look at one of the following links:
http://www.silverlightshow.net/items/Building-a-DataGrid-Control-for-Silverlight-for-Windows-Phone-Part-1.aspx
WpfToolkit DataGrid does not work in Windows Phone 7
ListBox with custom DataTempale is what you looking for.
First of all, download data from the server and put it into some collection. ObservableCollection is a best choice, because it's automatically update view when you add/remove new items. So, code snipped will be like this:
ObservableCollection<CustomItem> items = new ObservableCollection<CustomItem>();
// add items to the `items` list
list.ItemsSource = items; // bind items to the ListBox with a name 'list'
Xaml:
<ListBox x:Name="list">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDifenition Width="Auto" />
<ColumnDifenition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text={Binding Field1} />
<TextBlock Grid.Column="1" Text={Binding Field1} />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
How can I bind all the artists from the Artists collection to a ListBox in a PanoramaItem?
My xaml is as follows:
<controls:PanoramaItem Header="Artist" Name="Pan3">
<!--Double line list with image placeholder and text wrapping-->
<ListBox Name="artistLb" Margin="0,0,-12,0" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<!--Replace rectangle with image-->
<Rectangle Height="100" Width="100" Fill="#FFE5001b" Margin="12,0,9,0"/>
<StackPanel Width="311">
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PanoramaItem>
and xaml.cs code:
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
MediaLibrary library = new MediaLibrary();
int CountArtist = library.Artists.Count;
//binding the library.Artist to the Panorama item
}
Thanks!
In my answer I will assume you started from a Windows Phone Panorama Project and already added the reference to Microsoft.Xna.Framework to gain access to the media library.
When binding Ui object like the ListBox to code behind the best solution is to stick with the ViewModel approach that is already provided in the project. In your project you should find a MainViewModel. To this viewmodel add the following property:
private MediaLibrary _library;
public MediaLibrary Library
{
get
{
if (_library == null)
{
_library = new MediaLibrary();
}
return _library;
}
}
This property exposes the MediaLibrary to your xaml. The library is instantiated when called for the first time.
From your xaml it is now possible to bind to this property, I am only showing the ListBox.
<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Library.Artists}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Notice that I am binding the ListBox to the subproperty Artists of the Library property we just created in the viewmodel. I edited the ItemTemplate to show just one TextBlock that binds to the Artist name.
On your emulator you will just see 1 artist as an example, to test this solution with a real device you will have to use the WPConnect tool, which is explained here
I hope this gets you going for now, please let me know if any questions remain.
Have you tried?
artistLb.DataContext = library.Artists;