DataGridComboBoxColumn SelectedItem Bind to int property

Jun 26, 2009 at 7:24 PM

Trying to bind SelectedIndex of DataGridComboBoxColumn to an integer property. Works fine if property is a string and the string name matches the item in the ComboBox of course, but values in my case are int's that I would like to select the index. Seems simple enough. It does not appear to bind to an int as nicely as the standard .NET combo box does. I have read the thread on the bool binding, I think it has to be simpler than that, no?

Here is the property:

        public int GainMode

        {

            get

            {

                return m_iGainMode;

            }

               

            set

            {

                m_iGainMode = value;

            }

        }

Here is the Zaymel:

<WPFToolkit:DataGrid AutoGenerateColumns="False" Name="grdGain">
     <WPFToolkit:DataGrid.Columns>
      <WPFToolkit:DataGridComboBoxColumn Header ="Mode" Width="80"
                   SelectedItemBinding="{Binding Path=GainMode}"
                   SelectedValuePath="GainMode">
             <WPFToolkit:DataGridComboBoxColumn.ItemsSource>
                  <Collection:ArrayList>
                        <System:String>Automatic</System:String>
                             <System:String>Manual</System:String>
                        </Collection:ArrayList>
                  </WPFToolkit:DataGridComboBoxColumn.ItemsSource>
            </WPFToolkit:DataGridComboBoxColumn>
      </WPFToolkit:DataGrid.Columns>
</WPFToolkit:DataGrid>

Any suggestions?

Coordinator
Jun 26, 2009 at 9:56 PM

I assume that GainMode is the index of selected item into you items source. Here are few points to consider...

  • I dont think that SelectedValuePath or SelectedItemBinding are relavent here.
  • Use ElementStyle and EditingElementStyle on DataGridComboBoxColumn to Bind SelectedIndex to GainMode property.
  • Make sure that GainMode is either backed by a DependencyProperty or INotifyPropertyChanged
Jun 26, 2009 at 10:55 PM

Got you, Thanks, I am on it :)