New Posts New Posts RSS Feed: How to Populate Data?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

How to Populate Data?

 Post Reply Post Reply
Author
rosmena View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Mar-2011
Location: Philippines
Posts: 12
Post Options Post Options   Quote rosmena Quote  Post ReplyReply Direct Link To This Post 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 dataestion
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 306
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
robertg View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 15-Mar-2011
Location: California
Posts: 87
Post Options Post Options   Quote robertg Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
robertg View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 15-Mar-2011
Location: California
Posts: 87
Post Options Post Options   Quote robertg Quote  Post ReplyReply Direct Link To This Post Posted: 12-May-2011 at 9:48am
Or you could take Mark's advice. Hi Mark! You apparently type slightly faster than I do.
Back to Top
rosmena View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Mar-2011
Location: Philippines
Posts: 12
Post Options Post Options   Quote rosmena Quote  Post ReplyReply Direct Link To This Post 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..
Back to Top
rosmena View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Mar-2011
Location: Philippines
Posts: 12
Post Options Post Options   Quote rosmena Quote  Post ReplyReply Direct Link To This Post Posted: 12-May-2011 at 6:39pm
I haven't tried robertg approch yet..
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 306
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post Posted: 12-May-2011 at 6:45pm
Can you please show us the code? Are you populating the Customers collection?
Back to Top
rosmena View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Mar-2011
Location: Philippines
Posts: 12
Post Options Post Options   Quote rosmena Quote  Post ReplyReply Direct Link To This Post Posted: 12-May-2011 at 11:02pm
This is the code in VB and also the XAML
Back to Top
rosmena View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Mar-2011
Location: Philippines
Posts: 12
Post Options Post Options   Quote rosmena Quote  Post ReplyReply Direct Link To This Post Posted: 12-May-2011 at 11:06pm
Back to Top
rosmena View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Mar-2011
Location: Philippines
Posts: 12
Post Options Post Options   Quote rosmena Quote  Post ReplyReply Direct Link To This Post Posted: 12-May-2011 at 11:13pm
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 306
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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>



Back to Top
judith0829 View Drop Down
Newbie
Newbie
Avatar

Joined: 11-Apr-2011
Posts: 2
Post Options Post Options   Quote judith0829 Quote  Post ReplyReply Direct Link To This Post Posted: 15-May-2011 at 4:58am
i am getting an error
 
Error 1 'local' is an undeclared prefix. Line 6, position 10.
Back to Top
judith0829 View Drop Down
Newbie
Newbie
Avatar

Joined: 11-Apr-2011
Posts: 2
Post Options Post Options   Quote judith0829 Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 306
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post 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>


Back to Top
rosmena View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Mar-2011
Location: Philippines
Posts: 12
Post Options Post Options   Quote rosmena Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down