Print Page | Close Window

How to Populate Data?

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2672
Printed Date: 29-Jul-2026 at 8:30pm


Topic: How to Populate Data?
Posted By: rosmena
Subject: How to Populate Data?
Date 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



Replies:
Posted By: smi-mark
Date 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.


Posted By: robertg
Date 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


Posted By: robertg
Date Posted: 12-May-2011 at 9:48am
Or you could take Mark's advice. Hi Mark! You apparently type slightly faster than I do.


Posted By: rosmena
Date 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..


Posted By: rosmena
Date Posted: 12-May-2011 at 6:39pm
I haven't tried robertg approch yet..


Posted By: smi-mark
Date Posted: 12-May-2011 at 6:45pm
Can you please show us the code? Are you populating the Customers collection?


Posted By: rosmena
Date Posted: 12-May-2011 at 11:02pm
This is the code in VB and also the XAML


Posted By: rosmena
Date Posted: 12-May-2011 at 11:06pm


Posted By: rosmena
Date Posted: 12-May-2011 at 11:13pm


Posted By: smi-mark
Date 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>





Posted By: judith0829
Date Posted: 15-May-2011 at 4:58am
i am getting an error
 
Error 1 'local' is an undeclared prefix. Line 6, position 10.


Posted By: judith0829
Date 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


Posted By: smi-mark
Date 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>




Posted By: rosmena
Date 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.



Print Page | Close Window