views:

2498

answers:

1

EDIT:

The Answer I was looking for was....

<dataFormToolkit:DataField Label="Business Type:">
                                        <ComboBox x:Name="BusinessType" SelectedItem="{Binding BusinessType, Mode=TwoWay}" >
                                            <ComboBox.Items>
                                                <sys:String>Land</sys:String>
                                                <sys:String>Maritime</sys:String>
                                            </ComboBox.Items>
                                        </ComboBox>
                                    </dataFormToolkit:DataField>

Heres the link to the article

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 in order to add a better user experience to my application. The impression i've got from reading around the web is that this isnt as easy as it should be. My current textbox code looks like:

<dataFormToolkit:DataField>
   <TextBox Text="{Binding BusinessType, Mode=TwoWay}" />
</dataFormToolkit:DataField>

my attempt at something similar is as follows but incorrect:

<ComboBox>
  <ComboBox.Items>
    <ComboBoxItem Content="Maritime" IsSelected="{Binding BusinessType, Mode=TwoWay}" />
    <ComboBoxItem Content="Land" IsSelected="{Binding BusinessType, Mode=TwoWay}" />
  </ComboBox.Items>
</ComboBox>

NB/ I want the combobox to be populated by a list or an enum etc. (preferably a list). The contents of the combobox should have nothing to do with the database, just that when the user hits submit the selection made in the combobox gets persisted back to the database. It is also important that the combobox can read from the database and display the specific selection that has already been made if this is the case.

****EDIT:

Current setup of dataform bound to datagrid with editable businesstype field as a textbox (I want to replace this textbox with a combobox that has two selectable items).

 <!--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>

So how do i manipulate this code to use a combobox instead of a textbox?

Any help in doing this would be greatly appreciated.

+1  A: 

You should setup your binding to use the ComboBox's SelectedValue property.

<ComboBox SelectedValue="{Binding BusinessType, Mode=TwoWay}">...</ComboBox>

The problem with this is that the ListBox and ComboBox will use the Equals() method on the object in the SelectedItem so if the types do not match then the ComboBox will not set the appropriate item as selected. Therefore, BusinessType will need to be a string since you are using ComboBoxItem's and specifying string content.

If bound the ItemsSource of the ComboBox then you would use SelectedItem and it would actually be an entity type as well, in which case you have more flexability/control around what equals what.

markti
Hi sorry I am so confused by this. I have added the full block of code that I am currently using. Are you saying that I need to create a binding in my code behind and set the combobox itemsource property to this binding? Or have I missed something?
Goober
any thoughts....................?
Goober