C # Silverlight - Combobox with TwoWay binding?

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>

      

Here is the link to the article

I have a C # silverlight business application that uses the ado.net entity framework and domain service class to bind to my sql server database and retrieve data from / save data to my database. I am using the dataformtoolkit namespace for layout text boxes that can be edited / displayed data using TwoWay binding mode to provide read / write capability.

In some of the fields, I want to use a combobox instead of a textbox to add a more user-friendly interface to my application. The impression I got from reading it on the internet is that it is not as easy as it should be. My current textbox code looks like this:

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

      

my attempt at something similar looks like this:

<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 combo box to be filled with list or enumeration, etc. (preferably a list). The content of the combobox should not have anything to do with the database, only when the user hits the favorites, the selection made in the combobox is stored in 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 so.

**** EDIT:

Current setup of a datagrid-bound data form with an editable businesstype as a textbox (I want to replace this textbox with a combo box 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 on this would be appreciated.

+2


source to share


1 answer


You must set up your binding to use the ComboBox SelectedValue property.

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

      



The problem is that the ListBox and ComboBox will use the Equals () method on the object in the SelectedItem, so if the types don't match, the ComboBox will not set the corresponding item as selected. Therefore the BusinessType must be a string as you are using a ComboBoxItem and are specifying string content.

If you bind an ItemSource from a ComboBox, you have to use a SelectedItem and it will actually be an entity type, in which case you have more flexibility / control around what is equal.

+3


source







All Articles