views:

850

answers:

1

I have a c# silverlight business application that uses the ado.net entity framework and the domain service class to bind to my sql server database and pull data from/ persist data to my database. I have been using the dataformtoolkit namespace to layout textboxes which can be edited/display data, using a TwoWay binding mode in order to allow the read/write functionality.

On some of the fields I want to use a combobox instead of a textbox etc. All I want to do is have a combobox as part of a dataform, populate it with the contents of a generic list containing 3 strings and persist any changes back to my sql server database just like a textbox or a checkbox works.........

I cannot find a single working sample that isn't overly complex and seems way to "out there" - it's a binding, why is this so hard?!

Current setup of dataform:

<!--DataForm Declaration-->
 <dataFormToolkit:DataForm x:Name="dataForm1" Height="410" Width="331"
                 VerticalAlignment="Top"       
                 Header="Job Details"
                 CurrentItem="{Binding SelectedItem, ElementName=dataGrid1}" 
                 HorizontalAlignment="Left" >
           <dataFormToolkit:DataForm.EditTemplate>
                <DataTemplate>
                    <StackPanel>
                      <dataFormToolkit:DataField>
                        <TextBox Text="{Binding BusinessType, Mode=TwoWay}" />
                      </dataFormToolkit:DataField>
                    </StackPanel>
                </DataTemplate>
           </dataFormToolkit:DataForm.EditTemplate>
   </dataFormToolkit:DataForm>

Help needed bigtime, thanks in advance!

+2  A: 

We literally just had the same problem.

This is what we came up with:

Put your items in an ObservableCollection

Your list of strings (or any object, really) will need to be in a place available to the combobox.

public class ItemCollection : ObservableCollection<string>
{
    public ItemCollection ()
    {
        Add("Hi");
        Add("Howdy");
        Add("Hola");
    }
}

Make your list available to your combobox in your view (xaml)

Once you compile this class, in its own file, you will need to reference it. You will add an xmlns to your xaml file to the classlibrary you place it in an then add a static reference, like below, to your xaml.

...
xmlns:alibraryxmlnsfromabove="clr-namespace:MyProject;assembly=MyProject"
...


<!--This goes at the top of your page for resources>  
<navigation:Page.Resources>  
    <alibraryxmlnsfromabove:ItemCollection x:Key="ItemCollection"/>  
</navigation:Page.Resources>

Set your combobox up in your view (xaml)

We need to do two things with the combobox. Tell it where the list is and where to put the item it selects.

  • ItemsSource = Where the list is
  • SelectedItem = Where to put the item when we select it.

See Code:

<dataForm:DataForm.EditTemplate>
    <DataTemplate>
        <StackPanel>
            <dataForm:DataField Label="Choose One">
                <ComboBox 
                    ItemsSource="{StaticResource ItemCollection}" 
                    SelectedItem="{Binding Path=FieldNameHere, Mode=TwoWay}"/>                      
            </dataForm:DataField>
        </StackPanel>                                
    </DataTemplate>
</dataForm:DataForm.EditTemplate>

Some side notes:

The view will create a new instance of the ItemCollection, so you may want to have it grab items from a entity model, DB, or Singleton object.

The next step in this process is to use converters. Using converters will allow you to use objects other than primitive datatypes (we used strings in this sample). But, that is another question.

Good luck. Hopefully this gets you there.

Jeremiah
excellent answer. The ItemSource and Observable collection are the keys.
tdyen