DataGridComboBoxColumn Binding

Mar 31, 2011 at 4:28 PM

I am trying to Bind an object to a datagrid. An issue arose with columns that were bound to properties that expected an integer value, when the user deleted the contents of the cell, and moved away, it would try to bind a null value to that column and throw a databinding error behind the scenes, it would show up in the output while debugging but while live it would just lock up the grid (the offending cell would be blank and no cells would be acceptable). This was easily fixed by applying a custom converter to the databinding that looked for null or empty strings and converted them to zeroes.

 

With Combo Boxes it seems to be a little trickier. An example of one of my databound combo boxes is as follows

		<my:DataGridComboBoxColumn x:Name="Gauge" Header="Gauge" SelectedValueBinding="{Binding Gauge}">
                       	<my:DataGridComboBoxColumn.ElementStyle>
                                <Style TargetType="ComboBox">

                                    <Setter Property="ItemsSource" Value="{Binding Path=Gauges}"/>
                                    <Setter Property="SelectedValue" Value="{Binding Path=Gauge}"/>

                                </Style>
                            </my:DataGridComboBoxColumn.ElementStyle>
                            <my:DataGridComboBoxColumn.EditingElementStyle>
                                <Style TargetType="ComboBox">
                                    <Setter Property="IsEditable" Value="True"/>
                                    <Setter Property="ItemsSource" Value="{Binding Path=Gauges}"/>
                                    <Setter Property="SelectedValue" Value="{Binding Path=Gauge}"/>
                                    <Setter Property="IsDropDownOpen" Value="True"/>
                                </Style>
                            </my:DataGridComboBoxColumn.EditingElementStyle>
                        </my:DataGridComboBoxColumn>
It works fine until a null or empty value is attempted to be selected, and which point the cell gets a red background and the grid starts working (with no message in the output window). The converter method doesn't seem to work quite as good here. Is there a way I can capture what the user types and validate it before it gets sent to the object and messes everything up?
Thanks,
Mike