| Author |
Share Topic Topic Search Topic Options
|
rosmena
Newbie
Joined: 20-Mar-2011
Location: Philippines
Posts: 12
|
Post Options
Quote Reply
Topic: How to Populate Data? Posted: 11-May-2011 at 11:52pm |
I have a DataGrid which has a combobox control in it. My quistion is how can i populate my combobox with data  estion
|
 |
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 306
|
Post Options
Quote Reply
Posted: 12-May-2011 at 9:46am |
|
You need to set the ItemsSource of the ComboBox to be databound to a property such as "Customers" which is implemented as an ObservableCollection or some other kind of collection.
You then need to populate this collection with the list of customers.
<ComboBox SelectedItem={Binding Order.Customer}" ItemsSource="{Binding Customers}" />
public ObservableCollection<Customer> Customers { get; set; }
Something like that.
|
 |
robertg
IdeaBlade
Joined: 15-Mar-2011
Location: California
Posts: 87
|
Post Options
Quote Reply
Posted: 12-May-2011 at 9:47am |
|
For your combobox, you want to bind ItemsSource to the navigation property that contains the possibilities for that column., and the SelectedItem to a scalar property on the current item. We have a Silverlight code sample which demonstrates this:
http://drc.ideablade.com/xwiki/bin/view/Documentation/SimpleComboBox
|
 |
robertg
IdeaBlade
Joined: 15-Mar-2011
Location: California
Posts: 87
|
Post Options
Quote Reply
Posted: 12-May-2011 at 9:48am |
|
Or you could take Mark's advice. Hi Mark! You apparently type slightly faster than I do.
|
 |
rosmena
Newbie
Joined: 20-Mar-2011
Location: Philippines
Posts: 12
|
Post Options
Quote Reply
Posted: 12-May-2011 at 6:38pm |
I have done what Mark's advice and still can't populate combobox. I want to Display all the ContactName of the Customer Table in that combobox. Take note that the combobox is within a Datagrid.
As you can see were a VB programmer(WinForm) trying to migrate to WPF or Silverlight, we have no expierence in regards to XAML thats why were having a hard time on this.
Thank you for helping us..
|
 |
rosmena
Newbie
Joined: 20-Mar-2011
Location: Philippines
Posts: 12
|
Post Options
Quote Reply
Posted: 12-May-2011 at 6:39pm |
|
I haven't tried robertg approch yet..
|
 |
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 306
|
Post Options
Quote Reply
Posted: 12-May-2011 at 6:45pm |
|
Can you please show us the code? Are you populating the Customers collection?
|
 |
rosmena
Newbie
Joined: 20-Mar-2011
Location: Philippines
Posts: 12
|
Post Options
Quote Reply
Posted: 12-May-2011 at 11:02pm |
This is the code in VB and also the XAML
|
 |
rosmena
Newbie
Joined: 20-Mar-2011
Location: Philippines
Posts: 12
|
Post Options
Quote Reply
Posted: 12-May-2011 at 11:06pm |
|
|
 |
rosmena
Newbie
Joined: 20-Mar-2011
Location: Philippines
Posts: 12
|
Post Options
Quote Reply
Posted: 12-May-2011 at 11:13pm |
|
|
 |
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 306
|
Post Options
Quote Reply
Posted: 12-May-2011 at 11:26pm |
The DataContext of the DataGrid is the reason why you are not able to bind to orders. You need to have some way of binding back to the root datacontext (The ViewModel) This is not so easy in SL4 but will become much easier in SL5, for now you could do something like this: <UserControl.Resources>
<local:MainPageViewModel x:Key="vm" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource vm}">
<sdk:DataGrid ItemsSource="{Binding Orders}">
<sdk:DataGrid.Columns>
<sdk:DataGridTemplateColumn x:Name="CustomerColumn" Header="Customer">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Customers, Source={StaticResource vm}}" />
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>
|
 |
judith0829
Newbie
Joined: 11-Apr-2011
Posts: 2
|
Post Options
Quote Reply
Posted: 15-May-2011 at 4:58am |
i am getting an error
Error 1 'local' is an undeclared prefix. Line 6, position 10.
|
 |
judith0829
Newbie
Joined: 11-Apr-2011
Posts: 2
|
Post Options
Quote Reply
Posted: 15-May-2011 at 5:24am |
|
local:MainPageViewModel was not found.Verify that you are not missing an assembly reference. That all assembly reference has been build
|
 |
smi-mark
DevForce MVP
Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 306
|
Post Options
Quote Reply
Posted: 15-May-2011 at 7:05am |
Hi, local is just a namespace you need to setup, in my project called ComboBoxTest this is the whole xaml. Take a look at the bolded line, replace the ComboBoxTest with your namespace and that should fix it.
<UserControl x:Class="ComboBoxTest.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sdk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" xmlns:local="clr-namespace:ComboBoxTest" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources> <local:MainPageViewModel x:Key="vm" /> </UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource vm}"> <sdk:DataGrid ItemsSource="{Binding Orders}"> <sdk:DataGrid.Columns> <sdk:DataGridTemplateColumn x:Name="CustomerColumn" Header="Customer"> <sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate> <ComboBox ItemsSource="{Binding Customers, Source={StaticResource vm}}" /> </DataTemplate> </sdk:DataGridTemplateColumn.CellTemplate> </sdk:DataGridTemplateColumn> </sdk:DataGrid.Columns> </sdk:DataGrid> </Grid> </UserControl>
|
|
 |
rosmena
Newbie
Joined: 20-Mar-2011
Location: Philippines
Posts: 12
|
Post Options
Quote Reply
Posted: 15-May-2011 at 10:13pm |
|
Thank you so much Mark i could not have done it with out your Help. Since you are from Dallas i'm assuming you are a Maverick fan.
|
 |