tabina
tabina

Reputation: 1155

Element Binding on DataGridComboBoxColumn

Is it possible to use Element Binding with a DataGridComboBoxColumns selected item?

I have some object MyObject, that contains a property "ListOfSomeTypes" of type ObservableCollection. SomeType implements INotifyPropertyChanged.

A list of SomeType should be displayed in a DataGrid. For the "ListOfSomeTypes" I am looking for a solution to do the following: the DataGrid has two columns. One is a DataGridComboBoxColumn with a fixed List of integers, e.g. {0,1,2,3}. The ItemsSource of that column is set via Binding (property "ListOfIndices" which is not part of MyObject). The second column should display the content of "ListOfSomeTypes" at the selected index position (from the other column). So I thought I could just use a MultiConverter to check the values of both and select the correct value for display. The problem is, that the value that is supposed to be the index is never set (null), although the combobox shows the first item of my index list.

Since the indices are only recquired for display, I don't want to have a "SelectedIndex" property in my MyObject class. Is it possible to access the selected value of the combobox with element binding? Which property needs to be used (since SelectedValueBinding seems to be wrong)? Or is there a better way?

This is my code:

<DataGridCheckBoxColumn Header="SomeType">
   <DataGridCheckBoxColumn.Binding>
       <MultiBinding Converter="converters:SomeTypeCodeToBoolMultiConverter}">
         <Binding Path="ListOfSomeTypes" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
         <Binding ElementName="dgcbcSelectedIndex" Path="SelectedValueBinding" 
                         UpdateSourceTrigger="PropertyChanged"/>
       </MultiBinding>
   </DataGridCheckBoxColumn.Binding>
</DataGridCheckBoxColumn>
<DataGridComboBoxColumn x:Name="dgcbcSelectedIndex" Header="Indices">
   <DataGridComboBoxColumn.ElementStyle>
      <Style TargetType="ComboBox">
         <Setter Property="ItemsSource" Value="{Binding Path=Data.ListOfIndices, Source={StaticResource proxy}, UpdateSourceTrigger=PropertyChanged}" />
         <Setter Property="IsSynchronizedWithCurrentItem" Value="True"/> 
      </Style>
   </DataGridComboBoxColumn.ElementStyle>
   <DataGridComboBoxColumn.EditingElementStyle>
      <Style TargetType="ComboBox">
         <Setter Property="ItemsSource" Value="{Binding Path=Data.ListOfIndices, Source={StaticResource proxy}, UpdateSourceTrigger=PropertyChanged}" />
      </Style>
   </DataGridComboBoxColumn.EditingElementStyle>
 </DataGridComboBoxColumn>

The converter

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
  if (values == null || values.Length != 2)
    return null;

  var listOfSomeTypes= (ObservableCollection<SomeType>) values[0];
  if (listOfSomeTypes!= null)
  {
    int selectedIndex = 0;
    if (values[1] != null)
    {
      try { selectedIndex = (int)values[1];  }
      catch (Exception)  { }
    }

    if (listOfSomeTypes.Count > selectedIndex )
    {
      var someType= listOfSomeTypes[selectedIndex ];
      return someType == TypeA;
    }        
  }
  return null;
}

Thanks for your help!

Upvotes: 0

Views: 486

Answers (1)

mm8
mm8

Reputation: 169400

Is it possible to use Element Binding with a DataGridComboBoxColumns selected item?

Short answer: No.

The DataGridCheckBox column has no idea what "dgcbcSelectedIndex" is. A DataGridComboBoxColumn is not a visual element that gets added to the visual tree. It is a type that eventually creates a ComboBox element so this won't work.

What you should do is to bind the selected item/index/value of the ComboBox to a source property of your data object and then bind the CheckBox to the same source property. You cannot use ElementName.

Upvotes: 0

Related Questions